aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/perf/Makefile2
-rw-r--r--tools/perf/builtin-probe.c227
-rw-r--r--tools/perf/util/probe-event.c273
-rw-r--r--tools/perf/util/probe-event.h10
4 files changed, 294 insertions, 218 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 76e4b04d4080..f8537cf812c9 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -370,6 +370,7 @@ LIB_H += util/hist.h
370LIB_H += util/thread.h 370LIB_H += util/thread.h
371LIB_H += util/data_map.h 371LIB_H += util/data_map.h
372LIB_H += util/probe-finder.h 372LIB_H += util/probe-finder.h
373LIB_H += util/probe-event.h
373 374
374LIB_OBJS += util/abspath.o 375LIB_OBJS += util/abspath.o
375LIB_OBJS += util/alias.o 376LIB_OBJS += util/alias.o
@@ -412,6 +413,7 @@ LIB_OBJS += util/svghelper.o
412LIB_OBJS += util/sort.o 413LIB_OBJS += util/sort.o
413LIB_OBJS += util/hist.o 414LIB_OBJS += util/hist.o
414LIB_OBJS += util/data_map.o 415LIB_OBJS += util/data_map.o
416LIB_OBJS += util/probe-event.o
415 417
416BUILTIN_OBJS += builtin-annotate.o 418BUILTIN_OBJS += builtin-annotate.o
417 419
diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index 5f47e624e57e..bf20df2e816d 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -40,6 +40,7 @@
40#include "util/parse-options.h" 40#include "util/parse-options.h"
41#include "util/parse-events.h" /* For debugfs_path */ 41#include "util/parse-events.h" /* For debugfs_path */
42#include "util/probe-finder.h" 42#include "util/probe-finder.h"
43#include "util/probe-event.h"
43 44
44/* Default vmlinux search paths */ 45/* Default vmlinux search paths */
45#define NR_SEARCH_PATH 3 46#define NR_SEARCH_PATH 3
@@ -51,8 +52,6 @@ const char *default_search_path[NR_SEARCH_PATH] = {
51 52
52#define MAX_PATH_LEN 256 53#define MAX_PATH_LEN 256
53#define MAX_PROBES 128 54#define MAX_PROBES 128
54#define MAX_PROBE_ARGS 128
55#define PERFPROBE_GROUP "probe"
56 55
57/* Session management structure */ 56/* Session management structure */
58static struct { 57static struct {
@@ -63,155 +62,17 @@ static struct {
63 struct probe_point probes[MAX_PROBES]; 62 struct probe_point probes[MAX_PROBES];
64} session; 63} session;
65 64
66#define semantic_error(msg ...) die("Semantic error :" msg)
67
68/* Parse probe point. Return 1 if return probe */
69static void parse_probe_point(char *arg, struct probe_point *pp)
70{
71 char *ptr, *tmp;
72 char c, nc = 0;
73 /*
74 * <Syntax>
75 * perf probe SRC:LN
76 * perf probe FUNC[+OFFS|%return][@SRC]
77 */
78
79 ptr = strpbrk(arg, ":+@%");
80 if (ptr) {
81 nc = *ptr;
82 *ptr++ = '\0';
83 }
84
85 /* Check arg is function or file and copy it */
86 if (strchr(arg, '.')) /* File */
87 pp->file = strdup(arg);
88 else /* Function */
89 pp->function = strdup(arg);
90 DIE_IF(pp->file == NULL && pp->function == NULL);
91
92 /* Parse other options */
93 while (ptr) {
94 arg = ptr;
95 c = nc;
96 ptr = strpbrk(arg, ":+@%");
97 if (ptr) {
98 nc = *ptr;
99 *ptr++ = '\0';
100 }
101 switch (c) {
102 case ':': /* Line number */
103 pp->line = strtoul(arg, &tmp, 0);
104 if (*tmp != '\0')
105 semantic_error("There is non-digit charactor"
106 " in line number.");
107 break;
108 case '+': /* Byte offset from a symbol */
109 pp->offset = strtoul(arg, &tmp, 0);
110 if (*tmp != '\0')
111 semantic_error("There is non-digit charactor"
112 " in offset.");
113 break;
114 case '@': /* File name */
115 if (pp->file)
116 semantic_error("SRC@SRC is not allowed.");
117 pp->file = strdup(arg);
118 DIE_IF(pp->file == NULL);
119 if (ptr)
120 semantic_error("@SRC must be the last "
121 "option.");
122 break;
123 case '%': /* Probe places */
124 if (strcmp(arg, "return") == 0) {
125 pp->retprobe = 1;
126 } else /* Others not supported yet */
127 semantic_error("%%%s is not supported.", arg);
128 break;
129 default:
130 DIE_IF("Program has a bug.");
131 break;
132 }
133 }
134
135 /* Exclusion check */
136 if (pp->line && pp->offset)
137 semantic_error("Offset can't be used with line number.");
138 if (!pp->line && pp->file && !pp->function)
139 semantic_error("File always requires line number.");
140 if (pp->offset && !pp->function)
141 semantic_error("Offset requires an entry function.");
142 if (pp->retprobe && !pp->function)
143 semantic_error("Return probe requires an entry function.");
144 if ((pp->offset || pp->line) && pp->retprobe)
145 semantic_error("Offset/Line can't be used with return probe.");
146
147 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
148 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
149}
150
151/* Parse an event definition. Note that any error must die. */ 65/* Parse an event definition. Note that any error must die. */
152static void parse_probe_event(const char *str) 66static void parse_probe_event(const char *str)
153{ 67{
154 char *argv[MAX_PROBE_ARGS + 1]; /* probe + args */
155 int argc, i;
156 struct probe_point *pp = &session.probes[session.nr_probe]; 68 struct probe_point *pp = &session.probes[session.nr_probe];
157 69
158 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); 70 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
159 if (++session.nr_probe == MAX_PROBES) 71 if (++session.nr_probe == MAX_PROBES)
160 semantic_error("Too many probes"); 72 die("Too many probes (> %d) are specified.", MAX_PROBES);
161
162 /* Separate arguments, similar to argv_split */
163 argc = 0;
164 do {
165 /* Skip separators */
166 while (isspace(*str))
167 str++;
168
169 /* Add an argument */
170 if (*str != '\0') {
171 const char *s = str;
172 /* Check the limit number of arguments */
173 if (argc == MAX_PROBE_ARGS + 1)
174 semantic_error("Too many arguments");
175
176 /* Skip the argument */
177 while (!isspace(*str) && *str != '\0')
178 str++;
179
180 /* Duplicate the argument */
181 argv[argc] = strndup(s, str - s);
182 if (argv[argc] == NULL)
183 die("strndup");
184 pr_debug("argv[%d]=%s\n", argc, argv[argc]);
185 argc++;
186
187 }
188 } while (*str != '\0');
189 if (!argc)
190 semantic_error("An empty argument.");
191
192 /* Parse probe point */
193 parse_probe_point(argv[0], pp);
194 free(argv[0]);
195 if (pp->file || pp->line)
196 session.need_dwarf = 1;
197
198 /* Copy arguments */
199 pp->nr_args = argc - 1;
200 if (pp->nr_args > 0) {
201 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
202 if (!pp->args)
203 die("malloc");
204 memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args);
205 }
206 73
207 /* Ensure return probe has no C argument */ 74 /* Parse perf-probe event into probe_point */
208 for (i = 0; i < pp->nr_args; i++) 75 session.need_dwarf = parse_perf_probe_event(str, pp);
209 if (is_c_varname(pp->args[i])) {
210 if (pp->retprobe)
211 semantic_error("You can't specify local"
212 " variable for kretprobe");
213 session.need_dwarf = 1;
214 }
215 76
216 pr_debug("%d arguments\n", pp->nr_args); 77 pr_debug("%d arguments\n", pp->nr_args);
217} 78}
@@ -288,59 +149,15 @@ static const struct option options[] = {
288 "\t\tALN:\tAbsolute line number in file.\n" 149 "\t\tALN:\tAbsolute line number in file.\n"
289 "\t\tARG:\tProbe argument (local variable name or\n" 150 "\t\tARG:\tProbe argument (local variable name or\n"
290#endif 151#endif
291 "\t\t\tkprobe-tracer argument format is supported.)\n", 152 "\t\t\tkprobe-tracer argument format.)\n",
292 opt_add_probe_event), 153 opt_add_probe_event),
293 OPT_END() 154 OPT_END()
294}; 155};
295 156
296static int write_new_event(int fd, const char *buf)
297{
298 int ret;
299
300 ret = write(fd, buf, strlen(buf));
301 if (ret <= 0)
302 die("Failed to create event.");
303 else
304 printf("Added new event: %s\n", buf);
305
306 return ret;
307}
308
309#define MAX_CMDLEN 256
310
311static int synthesize_probe_event(struct probe_point *pp)
312{
313 char *buf;
314 int i, len, ret;
315 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
316 if (!buf)
317 die("Failed to allocate memory by zalloc.");
318 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
319 if (ret <= 0 || ret >= MAX_CMDLEN)
320 goto error;
321 len = ret;
322
323 for (i = 0; i < pp->nr_args; i++) {
324 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
325 pp->args[i]);
326 if (ret <= 0 || ret >= MAX_CMDLEN - len)
327 goto error;
328 len += ret;
329 }
330 pp->found = 1;
331 return pp->found;
332error:
333 free(pp->probes[0]);
334 if (ret > 0)
335 ret = -E2BIG;
336 return ret;
337}
338
339int cmd_probe(int argc, const char **argv, const char *prefix __used) 157int cmd_probe(int argc, const char **argv, const char *prefix __used)
340{ 158{
341 int i, j, fd, ret; 159 int i, j, fd, ret;
342 struct probe_point *pp; 160 struct probe_point *pp;
343 char buf[MAX_CMDLEN];
344 161
345 argc = parse_options(argc, argv, options, probe_usage, 162 argc = parse_options(argc, argv, options, probe_usage,
346 PARSE_OPT_STOP_AT_NON_OPTION); 163 PARSE_OPT_STOP_AT_NON_OPTION);
@@ -352,7 +169,7 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
352 169
353 if (session.need_dwarf) 170 if (session.need_dwarf)
354#ifdef NO_LIBDWARF 171#ifdef NO_LIBDWARF
355 semantic_error("Debuginfo-analysis is not supported"); 172 die("Debuginfo-analysis is not supported");
356#else /* !NO_LIBDWARF */ 173#else /* !NO_LIBDWARF */
357 pr_debug("Some probes require debuginfo.\n"); 174 pr_debug("Some probes require debuginfo.\n");
358 175
@@ -398,41 +215,15 @@ end_dwarf:
398 if (pp->found) /* This probe is already found. */ 215 if (pp->found) /* This probe is already found. */
399 continue; 216 continue;
400 217
401 ret = synthesize_probe_event(pp); 218 ret = synthesize_trace_kprobe_event(pp);
402 if (ret == -E2BIG) 219 if (ret == -E2BIG)
403 semantic_error("probe point is too long."); 220 die("probe point definition becomes too long.");
404 else if (ret < 0) 221 else if (ret < 0)
405 die("Failed to synthesize a probe point."); 222 die("Failed to synthesize a probe point.");
406 } 223 }
407 224
408 /* Settng up probe points */ 225 /* Settng up probe points */
409 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path); 226 add_trace_kprobe_events(session.probes, session.nr_probe);
410 fd = open(buf, O_WRONLY, O_APPEND);
411 if (fd < 0) {
412 if (errno == ENOENT)
413 die("kprobe_events file does not exist - please rebuild with CONFIG_KPROBE_TRACER.");
414 else
415 die("Could not open kprobe_events file: %s",
416 strerror(errno));
417 }
418 for (j = 0; j < session.nr_probe; j++) {
419 pp = &session.probes[j];
420 if (pp->found == 1) {
421 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n",
422 pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP,
423 pp->function, pp->offset, pp->probes[0]);
424 write_new_event(fd, buf);
425 } else
426 for (i = 0; i < pp->found; i++) {
427 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n",
428 pp->retprobe ? 'r' : 'p',
429 PERFPROBE_GROUP,
430 pp->function, pp->offset, i,
431 pp->probes[i]);
432 write_new_event(fd, buf);
433 }
434 }
435 close(fd);
436 return 0; 227 return 0;
437} 228}
438 229
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
new file mode 100644
index 000000000000..7335a3b5e494
--- /dev/null
+++ b/tools/perf/util/probe-event.c
@@ -0,0 +1,273 @@
1/*
2 * probe-event.c : perf-probe definition to kprobe_events format converter
3 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22#define _GNU_SOURCE
23#include <sys/utsname.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
32
33#undef _GNU_SOURCE
34#include "event.h"
35#include "debug.h"
36#include "parse-events.h" /* For debugfs_path */
37#include "probe-event.h"
38
39#define MAX_CMDLEN 256
40#define MAX_PROBE_ARGS 128
41#define PERFPROBE_GROUP "probe"
42
43#define semantic_error(msg ...) die("Semantic error :" msg)
44
45/* Parse probepoint definition. */
46static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
47{
48 char *ptr, *tmp;
49 char c, nc = 0;
50 /*
51 * <Syntax>
52 * perf probe SRC:LN
53 * perf probe FUNC[+OFFS|%return][@SRC]
54 */
55
56 ptr = strpbrk(arg, ":+@%");
57 if (ptr) {
58 nc = *ptr;
59 *ptr++ = '\0';
60 }
61
62 /* Check arg is function or file and copy it */
63 if (strchr(arg, '.')) /* File */
64 pp->file = strdup(arg);
65 else /* Function */
66 pp->function = strdup(arg);
67 DIE_IF(pp->file == NULL && pp->function == NULL);
68
69 /* Parse other options */
70 while (ptr) {
71 arg = ptr;
72 c = nc;
73 ptr = strpbrk(arg, ":+@%");
74 if (ptr) {
75 nc = *ptr;
76 *ptr++ = '\0';
77 }
78 switch (c) {
79 case ':': /* Line number */
80 pp->line = strtoul(arg, &tmp, 0);
81 if (*tmp != '\0')
82 semantic_error("There is non-digit charactor"
83 " in line number.");
84 break;
85 case '+': /* Byte offset from a symbol */
86 pp->offset = strtoul(arg, &tmp, 0);
87 if (*tmp != '\0')
88 semantic_error("There is non-digit charactor"
89 " in offset.");
90 break;
91 case '@': /* File name */
92 if (pp->file)
93 semantic_error("SRC@SRC is not allowed.");
94 pp->file = strdup(arg);
95 DIE_IF(pp->file == NULL);
96 if (ptr)
97 semantic_error("@SRC must be the last "
98 "option.");
99 break;
100 case '%': /* Probe places */
101 if (strcmp(arg, "return") == 0) {
102 pp->retprobe = 1;
103 } else /* Others not supported yet */
104 semantic_error("%%%s is not supported.", arg);
105 break;
106 default:
107 DIE_IF("Program has a bug.");
108 break;
109 }
110 }
111
112 /* Exclusion check */
113 if (pp->line && pp->offset)
114 semantic_error("Offset can't be used with line number.");
115
116 if (!pp->line && pp->file && !pp->function)
117 semantic_error("File always requires line number.");
118
119 if (pp->offset && !pp->function)
120 semantic_error("Offset requires an entry function.");
121
122 if (pp->retprobe && !pp->function)
123 semantic_error("Return probe requires an entry function.");
124
125 if ((pp->offset || pp->line) && pp->retprobe)
126 semantic_error("Offset/Line can't be used with return probe.");
127
128 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
129 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
130}
131
132/* Parse perf-probe event definition */
133int parse_perf_probe_event(const char *str, struct probe_point *pp)
134{
135 char *argv[MAX_PROBE_ARGS + 1]; /* probe + args */
136 int argc, i, need_dwarf = 0;
137
138 /* Separate arguments, similar to argv_split */
139 argc = 0;
140 do {
141 /* Skip separators */
142 while (isspace(*str))
143 str++;
144
145 /* Add an argument */
146 if (*str != '\0') {
147 const char *s = str;
148 /* Check the limit number of arguments */
149 if (argc == MAX_PROBE_ARGS + 1)
150 semantic_error("Too many arguments");
151
152 /* Skip the argument */
153 while (!isspace(*str) && *str != '\0')
154 str++;
155
156 /* Duplicate the argument */
157 argv[argc] = strndup(s, str - s);
158 if (argv[argc] == NULL)
159 die("strndup");
160 pr_debug("argv[%d]=%s\n", argc, argv[argc]);
161 argc++;
162 }
163 } while (*str != '\0');
164 if (!argc)
165 semantic_error("An empty argument.");
166
167 /* Parse probe point */
168 parse_perf_probe_probepoint(argv[0], pp);
169 free(argv[0]);
170 if (pp->file || pp->line)
171 need_dwarf = 1;
172
173 /* Copy arguments */
174 pp->nr_args = argc - 1;
175 if (pp->nr_args > 0) {
176 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
177 if (!pp->args)
178 die("malloc");
179 memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args);
180 }
181
182 /* Ensure return probe has no C argument */
183 for (i = 0; i < pp->nr_args; i++)
184 if (is_c_varname(pp->args[i])) {
185 if (pp->retprobe)
186 semantic_error("You can't specify local"
187 " variable for kretprobe");
188 need_dwarf = 1;
189 }
190
191 return need_dwarf;
192}
193
194int synthesize_trace_kprobe_event(struct probe_point *pp)
195{
196 char *buf;
197 int i, len, ret;
198
199 pp->probes[0] = buf = zalloc(MAX_CMDLEN);
200 if (!buf)
201 die("Failed to allocate memory by zalloc.");
202 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
203 if (ret <= 0 || ret >= MAX_CMDLEN)
204 goto error;
205 len = ret;
206
207 for (i = 0; i < pp->nr_args; i++) {
208 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
209 pp->args[i]);
210 if (ret <= 0 || ret >= MAX_CMDLEN - len)
211 goto error;
212 len += ret;
213 }
214 pp->found = 1;
215
216 return pp->found;
217error:
218 free(pp->probes[0]);
219 if (ret > 0)
220 ret = -E2BIG;
221
222 return ret;
223}
224
225static int write_trace_kprobe_event(int fd, const char *buf)
226{
227 int ret;
228
229 ret = write(fd, buf, strlen(buf));
230 if (ret <= 0)
231 die("Failed to create event.");
232 else
233 printf("Added new event: %s\n", buf);
234
235 return ret;
236}
237
238void add_trace_kprobe_events(struct probe_point *probes, int nr_probes)
239{
240 int i, j, fd;
241 struct probe_point *pp;
242 char buf[MAX_CMDLEN];
243
244 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
245 fd = open(buf, O_WRONLY, O_APPEND);
246 if (fd < 0) {
247 if (errno == ENOENT)
248 die("kprobe_events file does not exist -"
249 " please rebuild with CONFIG_KPROBE_TRACER.");
250 else
251 die("Could not open kprobe_events file: %s",
252 strerror(errno));
253 }
254
255 for (j = 0; j < nr_probes; j++) {
256 pp = probes + j;
257 if (pp->found == 1) {
258 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n",
259 pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP,
260 pp->function, pp->offset, pp->probes[0]);
261 write_trace_kprobe_event(fd, buf);
262 } else
263 for (i = 0; i < pp->found; i++) {
264 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n",
265 pp->retprobe ? 'r' : 'p',
266 PERFPROBE_GROUP,
267 pp->function, pp->offset, i,
268 pp->probes[i]);
269 write_trace_kprobe_event(fd, buf);
270 }
271 }
272 close(fd);
273}
diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
new file mode 100644
index 000000000000..0089c455ecac
--- /dev/null
+++ b/tools/perf/util/probe-event.h
@@ -0,0 +1,10 @@
1#ifndef _PROBE_EVENT_H
2#define _PROBE_EVENT_H
3
4#include "probe-finder.h"
5
6extern int parse_perf_probe_event(const char *str, struct probe_point *pp);
7extern int synthesize_trace_kprobe_event(struct probe_point *pp);
8extern void add_trace_kprobe_events(struct probe_point *probes, int nr_probes);
9
10#endif /*_PROBE_EVENT_H */