aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-probe.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/builtin-probe.c')
-rw-r--r--tools/perf/builtin-probe.c227
1 files changed, 9 insertions, 218 deletions
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