diff options
Diffstat (limited to 'tools/perf/util/probe-event.c')
-rw-r--r-- | tools/perf/util/probe-event.c | 1580 |
1 files changed, 1212 insertions, 368 deletions
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 7c004b6ef24f..914c67095d96 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c | |||
@@ -33,20 +33,27 @@ | |||
33 | #include <limits.h> | 33 | #include <limits.h> |
34 | 34 | ||
35 | #undef _GNU_SOURCE | 35 | #undef _GNU_SOURCE |
36 | #include "util.h" | ||
36 | #include "event.h" | 37 | #include "event.h" |
37 | #include "string.h" | 38 | #include "string.h" |
38 | #include "strlist.h" | 39 | #include "strlist.h" |
39 | #include "debug.h" | 40 | #include "debug.h" |
40 | #include "cache.h" | 41 | #include "cache.h" |
41 | #include "color.h" | 42 | #include "color.h" |
42 | #include "parse-events.h" /* For debugfs_path */ | 43 | #include "symbol.h" |
44 | #include "thread.h" | ||
45 | #include "debugfs.h" | ||
46 | #include "trace-event.h" /* For __unused */ | ||
43 | #include "probe-event.h" | 47 | #include "probe-event.h" |
48 | #include "probe-finder.h" | ||
44 | 49 | ||
45 | #define MAX_CMDLEN 256 | 50 | #define MAX_CMDLEN 256 |
46 | #define MAX_PROBE_ARGS 128 | 51 | #define MAX_PROBE_ARGS 128 |
47 | #define PERFPROBE_GROUP "probe" | 52 | #define PERFPROBE_GROUP "probe" |
48 | 53 | ||
49 | #define semantic_error(msg ...) die("Semantic error :" msg) | 54 | bool probe_event_dry_run; /* Dry run flag */ |
55 | |||
56 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) | ||
50 | 57 | ||
51 | /* If there is no space to write, returns -E2BIG. */ | 58 | /* If there is no space to write, returns -E2BIG. */ |
52 | static int e_snprintf(char *str, size_t size, const char *format, ...) | 59 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
@@ -64,7 +71,275 @@ static int e_snprintf(char *str, size_t size, const char *format, ...) | |||
64 | return ret; | 71 | return ret; |
65 | } | 72 | } |
66 | 73 | ||
67 | void parse_line_range_desc(const char *arg, struct line_range *lr) | 74 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp); |
75 | static struct machine machine; | ||
76 | |||
77 | /* Initialize symbol maps and path of vmlinux */ | ||
78 | static int init_vmlinux(void) | ||
79 | { | ||
80 | struct dso *kernel; | ||
81 | int ret; | ||
82 | |||
83 | symbol_conf.sort_by_name = true; | ||
84 | if (symbol_conf.vmlinux_name == NULL) | ||
85 | symbol_conf.try_vmlinux_path = true; | ||
86 | else | ||
87 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); | ||
88 | ret = symbol__init(); | ||
89 | if (ret < 0) { | ||
90 | pr_debug("Failed to init symbol map.\n"); | ||
91 | goto out; | ||
92 | } | ||
93 | |||
94 | ret = machine__init(&machine, "/", 0); | ||
95 | if (ret < 0) | ||
96 | goto out; | ||
97 | |||
98 | kernel = dso__new_kernel(symbol_conf.vmlinux_name); | ||
99 | if (kernel == NULL) | ||
100 | die("Failed to create kernel dso."); | ||
101 | |||
102 | ret = __machine__create_kernel_maps(&machine, kernel); | ||
103 | if (ret < 0) | ||
104 | pr_debug("Failed to create kernel maps.\n"); | ||
105 | |||
106 | out: | ||
107 | if (ret < 0) | ||
108 | pr_warning("Failed to init vmlinux path.\n"); | ||
109 | return ret; | ||
110 | } | ||
111 | |||
112 | #ifdef DWARF_SUPPORT | ||
113 | static int open_vmlinux(void) | ||
114 | { | ||
115 | if (map__load(machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) { | ||
116 | pr_debug("Failed to load kernel map.\n"); | ||
117 | return -EINVAL; | ||
118 | } | ||
119 | pr_debug("Try to open %s\n", machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name); | ||
120 | return open(machine.vmlinux_maps[MAP__FUNCTION]->dso->long_name, O_RDONLY); | ||
121 | } | ||
122 | |||
123 | /* Convert trace point to probe point with debuginfo */ | ||
124 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, | ||
125 | struct perf_probe_point *pp) | ||
126 | { | ||
127 | struct symbol *sym; | ||
128 | int fd, ret = -ENOENT; | ||
129 | |||
130 | sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION], | ||
131 | tp->symbol, NULL); | ||
132 | if (sym) { | ||
133 | fd = open_vmlinux(); | ||
134 | if (fd >= 0) { | ||
135 | ret = find_perf_probe_point(fd, | ||
136 | sym->start + tp->offset, pp); | ||
137 | close(fd); | ||
138 | } | ||
139 | } | ||
140 | if (ret <= 0) { | ||
141 | pr_debug("Failed to find corresponding probes from " | ||
142 | "debuginfo. Use kprobe event information.\n"); | ||
143 | pp->function = strdup(tp->symbol); | ||
144 | if (pp->function == NULL) | ||
145 | return -ENOMEM; | ||
146 | pp->offset = tp->offset; | ||
147 | } | ||
148 | pp->retprobe = tp->retprobe; | ||
149 | |||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | /* Try to find perf_probe_event with debuginfo */ | ||
154 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, | ||
155 | struct kprobe_trace_event **tevs, | ||
156 | int max_tevs) | ||
157 | { | ||
158 | bool need_dwarf = perf_probe_event_need_dwarf(pev); | ||
159 | int fd, ntevs; | ||
160 | |||
161 | fd = open_vmlinux(); | ||
162 | if (fd < 0) { | ||
163 | if (need_dwarf) { | ||
164 | pr_warning("Failed to open debuginfo file.\n"); | ||
165 | return fd; | ||
166 | } | ||
167 | pr_debug("Could not open vmlinux. Try to use symbols.\n"); | ||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | /* Searching trace events corresponding to probe event */ | ||
172 | ntevs = find_kprobe_trace_events(fd, pev, tevs, max_tevs); | ||
173 | close(fd); | ||
174 | |||
175 | if (ntevs > 0) { /* Succeeded to find trace events */ | ||
176 | pr_debug("find %d kprobe_trace_events.\n", ntevs); | ||
177 | return ntevs; | ||
178 | } | ||
179 | |||
180 | if (ntevs == 0) { /* No error but failed to find probe point. */ | ||
181 | pr_warning("Probe point '%s' not found.\n", | ||
182 | synthesize_perf_probe_point(&pev->point)); | ||
183 | return -ENOENT; | ||
184 | } | ||
185 | /* Error path : ntevs < 0 */ | ||
186 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); | ||
187 | if (ntevs == -EBADF) { | ||
188 | pr_warning("Warning: No dwarf info found in the vmlinux - " | ||
189 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); | ||
190 | if (!need_dwarf) { | ||
191 | pr_debug("Trying to use symbols.\nn"); | ||
192 | return 0; | ||
193 | } | ||
194 | } | ||
195 | return ntevs; | ||
196 | } | ||
197 | |||
198 | #define LINEBUF_SIZE 256 | ||
199 | #define NR_ADDITIONAL_LINES 2 | ||
200 | |||
201 | static int show_one_line(FILE *fp, int l, bool skip, bool show_num) | ||
202 | { | ||
203 | char buf[LINEBUF_SIZE]; | ||
204 | const char *color = PERF_COLOR_BLUE; | ||
205 | |||
206 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) | ||
207 | goto error; | ||
208 | if (!skip) { | ||
209 | if (show_num) | ||
210 | fprintf(stdout, "%7d %s", l, buf); | ||
211 | else | ||
212 | color_fprintf(stdout, color, " %s", buf); | ||
213 | } | ||
214 | |||
215 | while (strlen(buf) == LINEBUF_SIZE - 1 && | ||
216 | buf[LINEBUF_SIZE - 2] != '\n') { | ||
217 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) | ||
218 | goto error; | ||
219 | if (!skip) { | ||
220 | if (show_num) | ||
221 | fprintf(stdout, "%s", buf); | ||
222 | else | ||
223 | color_fprintf(stdout, color, "%s", buf); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | return 0; | ||
228 | error: | ||
229 | if (feof(fp)) | ||
230 | pr_warning("Source file is shorter than expected.\n"); | ||
231 | else | ||
232 | pr_warning("File read error: %s\n", strerror(errno)); | ||
233 | |||
234 | return -1; | ||
235 | } | ||
236 | |||
237 | /* | ||
238 | * Show line-range always requires debuginfo to find source file and | ||
239 | * line number. | ||
240 | */ | ||
241 | int show_line_range(struct line_range *lr) | ||
242 | { | ||
243 | int l = 1; | ||
244 | struct line_node *ln; | ||
245 | FILE *fp; | ||
246 | int fd, ret; | ||
247 | |||
248 | /* Search a line range */ | ||
249 | ret = init_vmlinux(); | ||
250 | if (ret < 0) | ||
251 | return ret; | ||
252 | |||
253 | fd = open_vmlinux(); | ||
254 | if (fd < 0) { | ||
255 | pr_warning("Failed to open debuginfo file.\n"); | ||
256 | return fd; | ||
257 | } | ||
258 | |||
259 | ret = find_line_range(fd, lr); | ||
260 | close(fd); | ||
261 | if (ret == 0) { | ||
262 | pr_warning("Specified source line is not found.\n"); | ||
263 | return -ENOENT; | ||
264 | } else if (ret < 0) { | ||
265 | pr_warning("Debuginfo analysis failed. (%d)\n", ret); | ||
266 | return ret; | ||
267 | } | ||
268 | |||
269 | setup_pager(); | ||
270 | |||
271 | if (lr->function) | ||
272 | fprintf(stdout, "<%s:%d>\n", lr->function, | ||
273 | lr->start - lr->offset); | ||
274 | else | ||
275 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); | ||
276 | |||
277 | fp = fopen(lr->path, "r"); | ||
278 | if (fp == NULL) { | ||
279 | pr_warning("Failed to open %s: %s\n", lr->path, | ||
280 | strerror(errno)); | ||
281 | return -errno; | ||
282 | } | ||
283 | /* Skip to starting line number */ | ||
284 | while (l < lr->start && ret >= 0) | ||
285 | ret = show_one_line(fp, l++, true, false); | ||
286 | if (ret < 0) | ||
287 | goto end; | ||
288 | |||
289 | list_for_each_entry(ln, &lr->line_list, list) { | ||
290 | while (ln->line > l && ret >= 0) | ||
291 | ret = show_one_line(fp, (l++) - lr->offset, | ||
292 | false, false); | ||
293 | if (ret >= 0) | ||
294 | ret = show_one_line(fp, (l++) - lr->offset, | ||
295 | false, true); | ||
296 | if (ret < 0) | ||
297 | goto end; | ||
298 | } | ||
299 | |||
300 | if (lr->end == INT_MAX) | ||
301 | lr->end = l + NR_ADDITIONAL_LINES; | ||
302 | while (l <= lr->end && !feof(fp) && ret >= 0) | ||
303 | ret = show_one_line(fp, (l++) - lr->offset, false, false); | ||
304 | end: | ||
305 | fclose(fp); | ||
306 | return ret; | ||
307 | } | ||
308 | |||
309 | #else /* !DWARF_SUPPORT */ | ||
310 | |||
311 | static int convert_to_perf_probe_point(struct kprobe_trace_point *tp, | ||
312 | struct perf_probe_point *pp) | ||
313 | { | ||
314 | pp->function = strdup(tp->symbol); | ||
315 | if (pp->function == NULL) | ||
316 | return -ENOMEM; | ||
317 | pp->offset = tp->offset; | ||
318 | pp->retprobe = tp->retprobe; | ||
319 | |||
320 | return 0; | ||
321 | } | ||
322 | |||
323 | static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, | ||
324 | struct kprobe_trace_event **tevs __unused, | ||
325 | int max_tevs __unused) | ||
326 | { | ||
327 | if (perf_probe_event_need_dwarf(pev)) { | ||
328 | pr_warning("Debuginfo-analysis is not supported.\n"); | ||
329 | return -ENOSYS; | ||
330 | } | ||
331 | return 0; | ||
332 | } | ||
333 | |||
334 | int show_line_range(struct line_range *lr __unused) | ||
335 | { | ||
336 | pr_warning("Debuginfo-analysis is not supported.\n"); | ||
337 | return -ENOSYS; | ||
338 | } | ||
339 | |||
340 | #endif | ||
341 | |||
342 | int parse_line_range_desc(const char *arg, struct line_range *lr) | ||
68 | { | 343 | { |
69 | const char *ptr; | 344 | const char *ptr; |
70 | char *tmp; | 345 | char *tmp; |
@@ -75,29 +350,45 @@ void parse_line_range_desc(const char *arg, struct line_range *lr) | |||
75 | */ | 350 | */ |
76 | ptr = strchr(arg, ':'); | 351 | ptr = strchr(arg, ':'); |
77 | if (ptr) { | 352 | if (ptr) { |
78 | lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0); | 353 | lr->start = (int)strtoul(ptr + 1, &tmp, 0); |
79 | if (*tmp == '+') | 354 | if (*tmp == '+') { |
80 | lr->end = lr->start + (unsigned int)strtoul(tmp + 1, | 355 | lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0); |
81 | &tmp, 0); | 356 | lr->end--; /* |
82 | else if (*tmp == '-') | 357 | * Adjust the number of lines here. |
83 | lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0); | 358 | * If the number of lines == 1, the |
359 | * the end of line should be equal to | ||
360 | * the start of line. | ||
361 | */ | ||
362 | } else if (*tmp == '-') | ||
363 | lr->end = (int)strtoul(tmp + 1, &tmp, 0); | ||
84 | else | 364 | else |
85 | lr->end = 0; | 365 | lr->end = INT_MAX; |
86 | pr_debug("Line range is %u to %u\n", lr->start, lr->end); | 366 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
87 | if (lr->end && lr->start > lr->end) | 367 | if (lr->start > lr->end) { |
88 | semantic_error("Start line must be smaller" | 368 | semantic_error("Start line must be smaller" |
89 | " than end line."); | 369 | " than end line.\n"); |
90 | if (*tmp != '\0') | 370 | return -EINVAL; |
91 | semantic_error("Tailing with invalid character '%d'.", | 371 | } |
372 | if (*tmp != '\0') { | ||
373 | semantic_error("Tailing with invalid character '%d'.\n", | ||
92 | *tmp); | 374 | *tmp); |
375 | return -EINVAL; | ||
376 | } | ||
93 | tmp = strndup(arg, (ptr - arg)); | 377 | tmp = strndup(arg, (ptr - arg)); |
94 | } else | 378 | } else { |
95 | tmp = strdup(arg); | 379 | tmp = strdup(arg); |
380 | lr->end = INT_MAX; | ||
381 | } | ||
382 | |||
383 | if (tmp == NULL) | ||
384 | return -ENOMEM; | ||
96 | 385 | ||
97 | if (strchr(tmp, '.')) | 386 | if (strchr(tmp, '.')) |
98 | lr->file = tmp; | 387 | lr->file = tmp; |
99 | else | 388 | else |
100 | lr->function = tmp; | 389 | lr->function = tmp; |
390 | |||
391 | return 0; | ||
101 | } | 392 | } |
102 | 393 | ||
103 | /* Check the name is good for event/group */ | 394 | /* Check the name is good for event/group */ |
@@ -113,8 +404,9 @@ static bool check_event_name(const char *name) | |||
113 | } | 404 | } |
114 | 405 | ||
115 | /* Parse probepoint definition. */ | 406 | /* Parse probepoint definition. */ |
116 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | 407 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
117 | { | 408 | { |
409 | struct perf_probe_point *pp = &pev->point; | ||
118 | char *ptr, *tmp; | 410 | char *ptr, *tmp; |
119 | char c, nc = 0; | 411 | char c, nc = 0; |
120 | /* | 412 | /* |
@@ -129,13 +421,19 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | |||
129 | if (ptr && *ptr == '=') { /* Event name */ | 421 | if (ptr && *ptr == '=') { /* Event name */ |
130 | *ptr = '\0'; | 422 | *ptr = '\0'; |
131 | tmp = ptr + 1; | 423 | tmp = ptr + 1; |
132 | ptr = strchr(arg, ':'); | 424 | if (strchr(arg, ':')) { |
133 | if (ptr) /* Group name is not supported yet. */ | 425 | semantic_error("Group name is not supported yet.\n"); |
134 | semantic_error("Group name is not supported yet."); | 426 | return -ENOTSUP; |
135 | if (!check_event_name(arg)) | 427 | } |
428 | if (!check_event_name(arg)) { | ||
136 | semantic_error("%s is bad for event name -it must " | 429 | semantic_error("%s is bad for event name -it must " |
137 | "follow C symbol-naming rule.", arg); | 430 | "follow C symbol-naming rule.\n", arg); |
138 | pp->event = strdup(arg); | 431 | return -EINVAL; |
432 | } | ||
433 | pev->event = strdup(arg); | ||
434 | if (pev->event == NULL) | ||
435 | return -ENOMEM; | ||
436 | pev->group = NULL; | ||
139 | arg = tmp; | 437 | arg = tmp; |
140 | } | 438 | } |
141 | 439 | ||
@@ -145,12 +443,15 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | |||
145 | *ptr++ = '\0'; | 443 | *ptr++ = '\0'; |
146 | } | 444 | } |
147 | 445 | ||
446 | tmp = strdup(arg); | ||
447 | if (tmp == NULL) | ||
448 | return -ENOMEM; | ||
449 | |||
148 | /* Check arg is function or file and copy it */ | 450 | /* Check arg is function or file and copy it */ |
149 | if (strchr(arg, '.')) /* File */ | 451 | if (strchr(tmp, '.')) /* File */ |
150 | pp->file = strdup(arg); | 452 | pp->file = tmp; |
151 | else /* Function */ | 453 | else /* Function */ |
152 | pp->function = strdup(arg); | 454 | pp->function = tmp; |
153 | DIE_IF(pp->file == NULL && pp->function == NULL); | ||
154 | 455 | ||
155 | /* Parse other options */ | 456 | /* Parse other options */ |
156 | while (ptr) { | 457 | while (ptr) { |
@@ -158,6 +459,8 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | |||
158 | c = nc; | 459 | c = nc; |
159 | if (c == ';') { /* Lazy pattern must be the last part */ | 460 | if (c == ';') { /* Lazy pattern must be the last part */ |
160 | pp->lazy_line = strdup(arg); | 461 | pp->lazy_line = strdup(arg); |
462 | if (pp->lazy_line == NULL) | ||
463 | return -ENOMEM; | ||
161 | break; | 464 | break; |
162 | } | 465 | } |
163 | ptr = strpbrk(arg, ";:+@%"); | 466 | ptr = strpbrk(arg, ";:+@%"); |
@@ -168,266 +471,658 @@ static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | |||
168 | switch (c) { | 471 | switch (c) { |
169 | case ':': /* Line number */ | 472 | case ':': /* Line number */ |
170 | pp->line = strtoul(arg, &tmp, 0); | 473 | pp->line = strtoul(arg, &tmp, 0); |
171 | if (*tmp != '\0') | 474 | if (*tmp != '\0') { |
172 | semantic_error("There is non-digit char" | 475 | semantic_error("There is non-digit char" |
173 | " in line number."); | 476 | " in line number.\n"); |
477 | return -EINVAL; | ||
478 | } | ||
174 | break; | 479 | break; |
175 | case '+': /* Byte offset from a symbol */ | 480 | case '+': /* Byte offset from a symbol */ |
176 | pp->offset = strtoul(arg, &tmp, 0); | 481 | pp->offset = strtoul(arg, &tmp, 0); |
177 | if (*tmp != '\0') | 482 | if (*tmp != '\0') { |
178 | semantic_error("There is non-digit character" | 483 | semantic_error("There is non-digit character" |
179 | " in offset."); | 484 | " in offset.\n"); |
485 | return -EINVAL; | ||
486 | } | ||
180 | break; | 487 | break; |
181 | case '@': /* File name */ | 488 | case '@': /* File name */ |
182 | if (pp->file) | 489 | if (pp->file) { |
183 | semantic_error("SRC@SRC is not allowed."); | 490 | semantic_error("SRC@SRC is not allowed.\n"); |
491 | return -EINVAL; | ||
492 | } | ||
184 | pp->file = strdup(arg); | 493 | pp->file = strdup(arg); |
185 | DIE_IF(pp->file == NULL); | 494 | if (pp->file == NULL) |
495 | return -ENOMEM; | ||
186 | break; | 496 | break; |
187 | case '%': /* Probe places */ | 497 | case '%': /* Probe places */ |
188 | if (strcmp(arg, "return") == 0) { | 498 | if (strcmp(arg, "return") == 0) { |
189 | pp->retprobe = 1; | 499 | pp->retprobe = 1; |
190 | } else /* Others not supported yet */ | 500 | } else { /* Others not supported yet */ |
191 | semantic_error("%%%s is not supported.", arg); | 501 | semantic_error("%%%s is not supported.\n", arg); |
502 | return -ENOTSUP; | ||
503 | } | ||
192 | break; | 504 | break; |
193 | default: | 505 | default: /* Buggy case */ |
194 | DIE_IF("Program has a bug."); | 506 | pr_err("This program has a bug at %s:%d.\n", |
507 | __FILE__, __LINE__); | ||
508 | return -ENOTSUP; | ||
195 | break; | 509 | break; |
196 | } | 510 | } |
197 | } | 511 | } |
198 | 512 | ||
199 | /* Exclusion check */ | 513 | /* Exclusion check */ |
200 | if (pp->lazy_line && pp->line) | 514 | if (pp->lazy_line && pp->line) { |
201 | semantic_error("Lazy pattern can't be used with line number."); | 515 | semantic_error("Lazy pattern can't be used with line number."); |
516 | return -EINVAL; | ||
517 | } | ||
202 | 518 | ||
203 | if (pp->lazy_line && pp->offset) | 519 | if (pp->lazy_line && pp->offset) { |
204 | semantic_error("Lazy pattern can't be used with offset."); | 520 | semantic_error("Lazy pattern can't be used with offset."); |
521 | return -EINVAL; | ||
522 | } | ||
205 | 523 | ||
206 | if (pp->line && pp->offset) | 524 | if (pp->line && pp->offset) { |
207 | semantic_error("Offset can't be used with line number."); | 525 | semantic_error("Offset can't be used with line number."); |
526 | return -EINVAL; | ||
527 | } | ||
208 | 528 | ||
209 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) | 529 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
210 | semantic_error("File always requires line number or " | 530 | semantic_error("File always requires line number or " |
211 | "lazy pattern."); | 531 | "lazy pattern."); |
532 | return -EINVAL; | ||
533 | } | ||
212 | 534 | ||
213 | if (pp->offset && !pp->function) | 535 | if (pp->offset && !pp->function) { |
214 | semantic_error("Offset requires an entry function."); | 536 | semantic_error("Offset requires an entry function."); |
537 | return -EINVAL; | ||
538 | } | ||
215 | 539 | ||
216 | if (pp->retprobe && !pp->function) | 540 | if (pp->retprobe && !pp->function) { |
217 | semantic_error("Return probe requires an entry function."); | 541 | semantic_error("Return probe requires an entry function."); |
542 | return -EINVAL; | ||
543 | } | ||
218 | 544 | ||
219 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) | 545 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
220 | semantic_error("Offset/Line/Lazy pattern can't be used with " | 546 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
221 | "return probe."); | 547 | "return probe."); |
548 | return -EINVAL; | ||
549 | } | ||
222 | 550 | ||
223 | pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n", | 551 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
224 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, | 552 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
225 | pp->lazy_line); | 553 | pp->lazy_line); |
554 | return 0; | ||
226 | } | 555 | } |
227 | 556 | ||
228 | /* Parse perf-probe event definition */ | 557 | /* Parse perf-probe event argument */ |
229 | void parse_perf_probe_event(const char *str, struct probe_point *pp, | 558 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
230 | bool *need_dwarf) | ||
231 | { | 559 | { |
232 | char **argv; | 560 | char *tmp; |
233 | int argc, i; | 561 | struct perf_probe_arg_field **fieldp; |
562 | |||
563 | pr_debug("parsing arg: %s into ", str); | ||
234 | 564 | ||
235 | *need_dwarf = false; | 565 | tmp = strchr(str, '='); |
566 | if (tmp) { | ||
567 | arg->name = strndup(str, tmp - str); | ||
568 | if (arg->name == NULL) | ||
569 | return -ENOMEM; | ||
570 | pr_debug("name:%s ", arg->name); | ||
571 | str = tmp + 1; | ||
572 | } | ||
236 | 573 | ||
237 | argv = argv_split(str, &argc); | 574 | tmp = strchr(str, ':'); |
238 | if (!argv) | 575 | if (tmp) { /* Type setting */ |
239 | die("argv_split failed."); | 576 | *tmp = '\0'; |
240 | if (argc > MAX_PROBE_ARGS + 1) | 577 | arg->type = strdup(tmp + 1); |
241 | semantic_error("Too many arguments"); | 578 | if (arg->type == NULL) |
579 | return -ENOMEM; | ||
580 | pr_debug("type:%s ", arg->type); | ||
581 | } | ||
242 | 582 | ||
583 | tmp = strpbrk(str, "-."); | ||
584 | if (!is_c_varname(str) || !tmp) { | ||
585 | /* A variable, register, symbol or special value */ | ||
586 | arg->var = strdup(str); | ||
587 | if (arg->var == NULL) | ||
588 | return -ENOMEM; | ||
589 | pr_debug("%s\n", arg->var); | ||
590 | return 0; | ||
591 | } | ||
592 | |||
593 | /* Structure fields */ | ||
594 | arg->var = strndup(str, tmp - str); | ||
595 | if (arg->var == NULL) | ||
596 | return -ENOMEM; | ||
597 | pr_debug("%s, ", arg->var); | ||
598 | fieldp = &arg->field; | ||
599 | |||
600 | do { | ||
601 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); | ||
602 | if (*fieldp == NULL) | ||
603 | return -ENOMEM; | ||
604 | if (*tmp == '.') { | ||
605 | str = tmp + 1; | ||
606 | (*fieldp)->ref = false; | ||
607 | } else if (tmp[1] == '>') { | ||
608 | str = tmp + 2; | ||
609 | (*fieldp)->ref = true; | ||
610 | } else { | ||
611 | semantic_error("Argument parse error: %s\n", str); | ||
612 | return -EINVAL; | ||
613 | } | ||
614 | |||
615 | tmp = strpbrk(str, "-."); | ||
616 | if (tmp) { | ||
617 | (*fieldp)->name = strndup(str, tmp - str); | ||
618 | if ((*fieldp)->name == NULL) | ||
619 | return -ENOMEM; | ||
620 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); | ||
621 | fieldp = &(*fieldp)->next; | ||
622 | } | ||
623 | } while (tmp); | ||
624 | (*fieldp)->name = strdup(str); | ||
625 | if ((*fieldp)->name == NULL) | ||
626 | return -ENOMEM; | ||
627 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); | ||
628 | |||
629 | /* If no name is specified, set the last field name */ | ||
630 | if (!arg->name) { | ||
631 | arg->name = strdup((*fieldp)->name); | ||
632 | if (arg->name == NULL) | ||
633 | return -ENOMEM; | ||
634 | } | ||
635 | return 0; | ||
636 | } | ||
637 | |||
638 | /* Parse perf-probe event command */ | ||
639 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) | ||
640 | { | ||
641 | char **argv; | ||
642 | int argc, i, ret = 0; | ||
643 | |||
644 | argv = argv_split(cmd, &argc); | ||
645 | if (!argv) { | ||
646 | pr_debug("Failed to split arguments.\n"); | ||
647 | return -ENOMEM; | ||
648 | } | ||
649 | if (argc - 1 > MAX_PROBE_ARGS) { | ||
650 | semantic_error("Too many probe arguments (%d).\n", argc - 1); | ||
651 | ret = -ERANGE; | ||
652 | goto out; | ||
653 | } | ||
243 | /* Parse probe point */ | 654 | /* Parse probe point */ |
244 | parse_perf_probe_probepoint(argv[0], pp); | 655 | ret = parse_perf_probe_point(argv[0], pev); |
245 | if (pp->file || pp->line || pp->lazy_line) | 656 | if (ret < 0) |
246 | *need_dwarf = true; | 657 | goto out; |
247 | 658 | ||
248 | /* Copy arguments and ensure return probe has no C argument */ | 659 | /* Copy arguments and ensure return probe has no C argument */ |
249 | pp->nr_args = argc - 1; | 660 | pev->nargs = argc - 1; |
250 | pp->args = zalloc(sizeof(char *) * pp->nr_args); | 661 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
251 | for (i = 0; i < pp->nr_args; i++) { | 662 | if (pev->args == NULL) { |
252 | pp->args[i] = strdup(argv[i + 1]); | 663 | ret = -ENOMEM; |
253 | if (!pp->args[i]) | 664 | goto out; |
254 | die("Failed to copy argument."); | 665 | } |
255 | if (is_c_varname(pp->args[i])) { | 666 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
256 | if (pp->retprobe) | 667 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
257 | semantic_error("You can't specify local" | 668 | if (ret >= 0 && |
258 | " variable for kretprobe"); | 669 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { |
259 | *need_dwarf = true; | 670 | semantic_error("You can't specify local variable for" |
671 | " kretprobe.\n"); | ||
672 | ret = -EINVAL; | ||
260 | } | 673 | } |
261 | } | 674 | } |
262 | 675 | out: | |
263 | argv_free(argv); | 676 | argv_free(argv); |
677 | |||
678 | return ret; | ||
679 | } | ||
680 | |||
681 | /* Return true if this perf_probe_event requires debuginfo */ | ||
682 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) | ||
683 | { | ||
684 | int i; | ||
685 | |||
686 | if (pev->point.file || pev->point.line || pev->point.lazy_line) | ||
687 | return true; | ||
688 | |||
689 | for (i = 0; i < pev->nargs; i++) | ||
690 | if (is_c_varname(pev->args[i].var)) | ||
691 | return true; | ||
692 | |||
693 | return false; | ||
264 | } | 694 | } |
265 | 695 | ||
266 | /* Parse kprobe_events event into struct probe_point */ | 696 | /* Parse kprobe_events event into struct probe_point */ |
267 | void parse_trace_kprobe_event(const char *str, struct probe_point *pp) | 697 | int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev) |
268 | { | 698 | { |
699 | struct kprobe_trace_point *tp = &tev->point; | ||
269 | char pr; | 700 | char pr; |
270 | char *p; | 701 | char *p; |
271 | int ret, i, argc; | 702 | int ret, i, argc; |
272 | char **argv; | 703 | char **argv; |
273 | 704 | ||
274 | pr_debug("Parsing kprobe_events: %s\n", str); | 705 | pr_debug("Parsing kprobe_events: %s\n", cmd); |
275 | argv = argv_split(str, &argc); | 706 | argv = argv_split(cmd, &argc); |
276 | if (!argv) | 707 | if (!argv) { |
277 | die("argv_split failed."); | 708 | pr_debug("Failed to split arguments.\n"); |
278 | if (argc < 2) | 709 | return -ENOMEM; |
279 | semantic_error("Too less arguments."); | 710 | } |
711 | if (argc < 2) { | ||
712 | semantic_error("Too few probe arguments.\n"); | ||
713 | ret = -ERANGE; | ||
714 | goto out; | ||
715 | } | ||
280 | 716 | ||
281 | /* Scan event and group name. */ | 717 | /* Scan event and group name. */ |
282 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", | 718 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
283 | &pr, (float *)(void *)&pp->group, | 719 | &pr, (float *)(void *)&tev->group, |
284 | (float *)(void *)&pp->event); | 720 | (float *)(void *)&tev->event); |
285 | if (ret != 3) | 721 | if (ret != 3) { |
286 | semantic_error("Failed to parse event name: %s", argv[0]); | 722 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
287 | pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr); | 723 | ret = -EINVAL; |
724 | goto out; | ||
725 | } | ||
726 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); | ||
288 | 727 | ||
289 | pp->retprobe = (pr == 'r'); | 728 | tp->retprobe = (pr == 'r'); |
290 | 729 | ||
291 | /* Scan function name and offset */ | 730 | /* Scan function name and offset */ |
292 | ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, | 731 | ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol, |
293 | &pp->offset); | 732 | &tp->offset); |
294 | if (ret == 1) | 733 | if (ret == 1) |
295 | pp->offset = 0; | 734 | tp->offset = 0; |
296 | |||
297 | /* kprobe_events doesn't have this information */ | ||
298 | pp->line = 0; | ||
299 | pp->file = NULL; | ||
300 | 735 | ||
301 | pp->nr_args = argc - 2; | 736 | tev->nargs = argc - 2; |
302 | pp->args = zalloc(sizeof(char *) * pp->nr_args); | 737 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); |
303 | for (i = 0; i < pp->nr_args; i++) { | 738 | if (tev->args == NULL) { |
739 | ret = -ENOMEM; | ||
740 | goto out; | ||
741 | } | ||
742 | for (i = 0; i < tev->nargs; i++) { | ||
304 | p = strchr(argv[i + 2], '='); | 743 | p = strchr(argv[i + 2], '='); |
305 | if (p) /* We don't need which register is assigned. */ | 744 | if (p) /* We don't need which register is assigned. */ |
306 | *p = '\0'; | 745 | *p++ = '\0'; |
307 | pp->args[i] = strdup(argv[i + 2]); | 746 | else |
308 | if (!pp->args[i]) | 747 | p = argv[i + 2]; |
309 | die("Failed to copy argument."); | 748 | tev->args[i].name = strdup(argv[i + 2]); |
749 | /* TODO: parse regs and offset */ | ||
750 | tev->args[i].value = strdup(p); | ||
751 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { | ||
752 | ret = -ENOMEM; | ||
753 | goto out; | ||
754 | } | ||
310 | } | 755 | } |
311 | 756 | ret = 0; | |
757 | out: | ||
312 | argv_free(argv); | 758 | argv_free(argv); |
759 | return ret; | ||
313 | } | 760 | } |
314 | 761 | ||
315 | /* Synthesize only probe point (not argument) */ | 762 | /* Compose only probe arg */ |
316 | int synthesize_perf_probe_point(struct probe_point *pp) | 763 | int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) |
317 | { | 764 | { |
318 | char *buf; | 765 | struct perf_probe_arg_field *field = pa->field; |
319 | char offs[64] = "", line[64] = ""; | ||
320 | int ret; | 766 | int ret; |
767 | char *tmp = buf; | ||
321 | 768 | ||
322 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); | 769 | if (pa->name && pa->var) |
323 | pp->found = 1; | 770 | ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); |
324 | if (!buf) | 771 | else |
325 | die("Failed to allocate memory by zalloc."); | 772 | ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); |
773 | if (ret <= 0) | ||
774 | goto error; | ||
775 | tmp += ret; | ||
776 | len -= ret; | ||
777 | |||
778 | while (field) { | ||
779 | ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".", | ||
780 | field->name); | ||
781 | if (ret <= 0) | ||
782 | goto error; | ||
783 | tmp += ret; | ||
784 | len -= ret; | ||
785 | field = field->next; | ||
786 | } | ||
787 | |||
788 | if (pa->type) { | ||
789 | ret = e_snprintf(tmp, len, ":%s", pa->type); | ||
790 | if (ret <= 0) | ||
791 | goto error; | ||
792 | tmp += ret; | ||
793 | len -= ret; | ||
794 | } | ||
795 | |||
796 | return tmp - buf; | ||
797 | error: | ||
798 | pr_debug("Failed to synthesize perf probe argument: %s", | ||
799 | strerror(-ret)); | ||
800 | return ret; | ||
801 | } | ||
802 | |||
803 | /* Compose only probe point (not argument) */ | ||
804 | static char *synthesize_perf_probe_point(struct perf_probe_point *pp) | ||
805 | { | ||
806 | char *buf, *tmp; | ||
807 | char offs[32] = "", line[32] = "", file[32] = ""; | ||
808 | int ret, len; | ||
809 | |||
810 | buf = zalloc(MAX_CMDLEN); | ||
811 | if (buf == NULL) { | ||
812 | ret = -ENOMEM; | ||
813 | goto error; | ||
814 | } | ||
326 | if (pp->offset) { | 815 | if (pp->offset) { |
327 | ret = e_snprintf(offs, 64, "+%d", pp->offset); | 816 | ret = e_snprintf(offs, 32, "+%lu", pp->offset); |
328 | if (ret <= 0) | 817 | if (ret <= 0) |
329 | goto error; | 818 | goto error; |
330 | } | 819 | } |
331 | if (pp->line) { | 820 | if (pp->line) { |
332 | ret = e_snprintf(line, 64, ":%d", pp->line); | 821 | ret = e_snprintf(line, 32, ":%d", pp->line); |
822 | if (ret <= 0) | ||
823 | goto error; | ||
824 | } | ||
825 | if (pp->file) { | ||
826 | len = strlen(pp->file) - 31; | ||
827 | if (len < 0) | ||
828 | len = 0; | ||
829 | tmp = strchr(pp->file + len, '/'); | ||
830 | if (!tmp) | ||
831 | tmp = pp->file + len; | ||
832 | ret = e_snprintf(file, 32, "@%s", tmp + 1); | ||
333 | if (ret <= 0) | 833 | if (ret <= 0) |
334 | goto error; | 834 | goto error; |
335 | } | 835 | } |
336 | 836 | ||
337 | if (pp->function) | 837 | if (pp->function) |
338 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function, | 838 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, |
339 | offs, pp->retprobe ? "%return" : "", line); | 839 | offs, pp->retprobe ? "%return" : "", line, |
840 | file); | ||
340 | else | 841 | else |
341 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line); | 842 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); |
342 | if (ret <= 0) { | 843 | if (ret <= 0) |
844 | goto error; | ||
845 | |||
846 | return buf; | ||
343 | error: | 847 | error: |
344 | free(pp->probes[0]); | 848 | pr_debug("Failed to synthesize perf probe point: %s", |
345 | pp->probes[0] = NULL; | 849 | strerror(-ret)); |
346 | pp->found = 0; | 850 | if (buf) |
347 | } | 851 | free(buf); |
348 | return ret; | 852 | return NULL; |
349 | } | 853 | } |
350 | 854 | ||
351 | int synthesize_perf_probe_event(struct probe_point *pp) | 855 | #if 0 |
856 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) | ||
352 | { | 857 | { |
353 | char *buf; | 858 | char *buf; |
354 | int i, len, ret; | 859 | int i, len, ret; |
355 | 860 | ||
356 | len = synthesize_perf_probe_point(pp); | 861 | buf = synthesize_perf_probe_point(&pev->point); |
357 | if (len < 0) | 862 | if (!buf) |
358 | return 0; | 863 | return NULL; |
359 | 864 | ||
360 | buf = pp->probes[0]; | 865 | len = strlen(buf); |
361 | for (i = 0; i < pp->nr_args; i++) { | 866 | for (i = 0; i < pev->nargs; i++) { |
362 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", | 867 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
363 | pp->args[i]); | 868 | pev->args[i].name); |
364 | if (ret <= 0) | 869 | if (ret <= 0) { |
365 | goto error; | 870 | free(buf); |
871 | return NULL; | ||
872 | } | ||
366 | len += ret; | 873 | len += ret; |
367 | } | 874 | } |
368 | pp->found = 1; | ||
369 | 875 | ||
370 | return pp->found; | 876 | return buf; |
371 | error: | 877 | } |
372 | free(pp->probes[0]); | 878 | #endif |
373 | pp->probes[0] = NULL; | 879 | |
880 | static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref, | ||
881 | char **buf, size_t *buflen, | ||
882 | int depth) | ||
883 | { | ||
884 | int ret; | ||
885 | if (ref->next) { | ||
886 | depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf, | ||
887 | buflen, depth + 1); | ||
888 | if (depth < 0) | ||
889 | goto out; | ||
890 | } | ||
891 | |||
892 | ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); | ||
893 | if (ret < 0) | ||
894 | depth = ret; | ||
895 | else { | ||
896 | *buf += ret; | ||
897 | *buflen -= ret; | ||
898 | } | ||
899 | out: | ||
900 | return depth; | ||
374 | 901 | ||
375 | return ret; | ||
376 | } | 902 | } |
377 | 903 | ||
378 | int synthesize_trace_kprobe_event(struct probe_point *pp) | 904 | static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg, |
905 | char *buf, size_t buflen) | ||
379 | { | 906 | { |
907 | int ret, depth = 0; | ||
908 | char *tmp = buf; | ||
909 | |||
910 | /* Argument name or separator */ | ||
911 | if (arg->name) | ||
912 | ret = e_snprintf(buf, buflen, " %s=", arg->name); | ||
913 | else | ||
914 | ret = e_snprintf(buf, buflen, " "); | ||
915 | if (ret < 0) | ||
916 | return ret; | ||
917 | buf += ret; | ||
918 | buflen -= ret; | ||
919 | |||
920 | /* Dereferencing arguments */ | ||
921 | if (arg->ref) { | ||
922 | depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf, | ||
923 | &buflen, 1); | ||
924 | if (depth < 0) | ||
925 | return depth; | ||
926 | } | ||
927 | |||
928 | /* Print argument value */ | ||
929 | ret = e_snprintf(buf, buflen, "%s", arg->value); | ||
930 | if (ret < 0) | ||
931 | return ret; | ||
932 | buf += ret; | ||
933 | buflen -= ret; | ||
934 | |||
935 | /* Closing */ | ||
936 | while (depth--) { | ||
937 | ret = e_snprintf(buf, buflen, ")"); | ||
938 | if (ret < 0) | ||
939 | return ret; | ||
940 | buf += ret; | ||
941 | buflen -= ret; | ||
942 | } | ||
943 | /* Print argument type */ | ||
944 | if (arg->type) { | ||
945 | ret = e_snprintf(buf, buflen, ":%s", arg->type); | ||
946 | if (ret <= 0) | ||
947 | return ret; | ||
948 | buf += ret; | ||
949 | } | ||
950 | |||
951 | return buf - tmp; | ||
952 | } | ||
953 | |||
954 | char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev) | ||
955 | { | ||
956 | struct kprobe_trace_point *tp = &tev->point; | ||
380 | char *buf; | 957 | char *buf; |
381 | int i, len, ret; | 958 | int i, len, ret; |
382 | 959 | ||
383 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); | 960 | buf = zalloc(MAX_CMDLEN); |
384 | if (!buf) | 961 | if (buf == NULL) |
385 | die("Failed to allocate memory by zalloc."); | 962 | return NULL; |
386 | ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); | 963 | |
387 | if (ret <= 0) | 964 | len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu", |
965 | tp->retprobe ? 'r' : 'p', | ||
966 | tev->group, tev->event, | ||
967 | tp->symbol, tp->offset); | ||
968 | if (len <= 0) | ||
388 | goto error; | 969 | goto error; |
389 | len = ret; | ||
390 | 970 | ||
391 | for (i = 0; i < pp->nr_args; i++) { | 971 | for (i = 0; i < tev->nargs; i++) { |
392 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", | 972 | ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len, |
393 | pp->args[i]); | 973 | MAX_CMDLEN - len); |
394 | if (ret <= 0) | 974 | if (ret <= 0) |
395 | goto error; | 975 | goto error; |
396 | len += ret; | 976 | len += ret; |
397 | } | 977 | } |
398 | pp->found = 1; | ||
399 | 978 | ||
400 | return pp->found; | 979 | return buf; |
401 | error: | 980 | error: |
402 | free(pp->probes[0]); | 981 | free(buf); |
403 | pp->probes[0] = NULL; | 982 | return NULL; |
983 | } | ||
984 | |||
985 | int convert_to_perf_probe_event(struct kprobe_trace_event *tev, | ||
986 | struct perf_probe_event *pev) | ||
987 | { | ||
988 | char buf[64] = ""; | ||
989 | int i, ret; | ||
990 | |||
991 | /* Convert event/group name */ | ||
992 | pev->event = strdup(tev->event); | ||
993 | pev->group = strdup(tev->group); | ||
994 | if (pev->event == NULL || pev->group == NULL) | ||
995 | return -ENOMEM; | ||
996 | |||
997 | /* Convert trace_point to probe_point */ | ||
998 | ret = convert_to_perf_probe_point(&tev->point, &pev->point); | ||
999 | if (ret < 0) | ||
1000 | return ret; | ||
1001 | |||
1002 | /* Convert trace_arg to probe_arg */ | ||
1003 | pev->nargs = tev->nargs; | ||
1004 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); | ||
1005 | if (pev->args == NULL) | ||
1006 | return -ENOMEM; | ||
1007 | for (i = 0; i < tev->nargs && ret >= 0; i++) { | ||
1008 | if (tev->args[i].name) | ||
1009 | pev->args[i].name = strdup(tev->args[i].name); | ||
1010 | else { | ||
1011 | ret = synthesize_kprobe_trace_arg(&tev->args[i], | ||
1012 | buf, 64); | ||
1013 | pev->args[i].name = strdup(buf); | ||
1014 | } | ||
1015 | if (pev->args[i].name == NULL && ret >= 0) | ||
1016 | ret = -ENOMEM; | ||
1017 | } | ||
1018 | |||
1019 | if (ret < 0) | ||
1020 | clear_perf_probe_event(pev); | ||
404 | 1021 | ||
405 | return ret; | 1022 | return ret; |
406 | } | 1023 | } |
407 | 1024 | ||
408 | static int open_kprobe_events(int flags, int mode) | 1025 | void clear_perf_probe_event(struct perf_probe_event *pev) |
1026 | { | ||
1027 | struct perf_probe_point *pp = &pev->point; | ||
1028 | struct perf_probe_arg_field *field, *next; | ||
1029 | int i; | ||
1030 | |||
1031 | if (pev->event) | ||
1032 | free(pev->event); | ||
1033 | if (pev->group) | ||
1034 | free(pev->group); | ||
1035 | if (pp->file) | ||
1036 | free(pp->file); | ||
1037 | if (pp->function) | ||
1038 | free(pp->function); | ||
1039 | if (pp->lazy_line) | ||
1040 | free(pp->lazy_line); | ||
1041 | for (i = 0; i < pev->nargs; i++) { | ||
1042 | if (pev->args[i].name) | ||
1043 | free(pev->args[i].name); | ||
1044 | if (pev->args[i].var) | ||
1045 | free(pev->args[i].var); | ||
1046 | if (pev->args[i].type) | ||
1047 | free(pev->args[i].type); | ||
1048 | field = pev->args[i].field; | ||
1049 | while (field) { | ||
1050 | next = field->next; | ||
1051 | if (field->name) | ||
1052 | free(field->name); | ||
1053 | free(field); | ||
1054 | field = next; | ||
1055 | } | ||
1056 | } | ||
1057 | if (pev->args) | ||
1058 | free(pev->args); | ||
1059 | memset(pev, 0, sizeof(*pev)); | ||
1060 | } | ||
1061 | |||
1062 | void clear_kprobe_trace_event(struct kprobe_trace_event *tev) | ||
1063 | { | ||
1064 | struct kprobe_trace_arg_ref *ref, *next; | ||
1065 | int i; | ||
1066 | |||
1067 | if (tev->event) | ||
1068 | free(tev->event); | ||
1069 | if (tev->group) | ||
1070 | free(tev->group); | ||
1071 | if (tev->point.symbol) | ||
1072 | free(tev->point.symbol); | ||
1073 | for (i = 0; i < tev->nargs; i++) { | ||
1074 | if (tev->args[i].name) | ||
1075 | free(tev->args[i].name); | ||
1076 | if (tev->args[i].value) | ||
1077 | free(tev->args[i].value); | ||
1078 | if (tev->args[i].type) | ||
1079 | free(tev->args[i].type); | ||
1080 | ref = tev->args[i].ref; | ||
1081 | while (ref) { | ||
1082 | next = ref->next; | ||
1083 | free(ref); | ||
1084 | ref = next; | ||
1085 | } | ||
1086 | } | ||
1087 | if (tev->args) | ||
1088 | free(tev->args); | ||
1089 | memset(tev, 0, sizeof(*tev)); | ||
1090 | } | ||
1091 | |||
1092 | static int open_kprobe_events(bool readwrite) | ||
409 | { | 1093 | { |
410 | char buf[PATH_MAX]; | 1094 | char buf[PATH_MAX]; |
1095 | const char *__debugfs; | ||
411 | int ret; | 1096 | int ret; |
412 | 1097 | ||
413 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); | 1098 | __debugfs = debugfs_find_mountpoint(); |
414 | if (ret < 0) | 1099 | if (__debugfs == NULL) { |
415 | die("Failed to make kprobe_events path."); | 1100 | pr_warning("Debugfs is not mounted.\n"); |
1101 | return -ENOENT; | ||
1102 | } | ||
1103 | |||
1104 | ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs); | ||
1105 | if (ret >= 0) { | ||
1106 | pr_debug("Opening %s write=%d\n", buf, readwrite); | ||
1107 | if (readwrite && !probe_event_dry_run) | ||
1108 | ret = open(buf, O_RDWR, O_APPEND); | ||
1109 | else | ||
1110 | ret = open(buf, O_RDONLY, 0); | ||
1111 | } | ||
416 | 1112 | ||
417 | ret = open(buf, flags, mode); | ||
418 | if (ret < 0) { | 1113 | if (ret < 0) { |
419 | if (errno == ENOENT) | 1114 | if (errno == ENOENT) |
420 | die("kprobe_events file does not exist -" | 1115 | pr_warning("kprobe_events file does not exist - please" |
421 | " please rebuild with CONFIG_KPROBE_EVENT."); | 1116 | " rebuild kernel with CONFIG_KPROBE_EVENT.\n"); |
422 | else | 1117 | else |
423 | die("Could not open kprobe_events file: %s", | 1118 | pr_warning("Failed to open kprobe_events file: %s\n", |
424 | strerror(errno)); | 1119 | strerror(errno)); |
425 | } | 1120 | } |
426 | return ret; | 1121 | return ret; |
427 | } | 1122 | } |
428 | 1123 | ||
429 | /* Get raw string list of current kprobe_events */ | 1124 | /* Get raw string list of current kprobe_events */ |
430 | static struct strlist *get_trace_kprobe_event_rawlist(int fd) | 1125 | static struct strlist *get_kprobe_trace_command_rawlist(int fd) |
431 | { | 1126 | { |
432 | int ret, idx; | 1127 | int ret, idx; |
433 | FILE *fp; | 1128 | FILE *fp; |
@@ -447,271 +1142,486 @@ static struct strlist *get_trace_kprobe_event_rawlist(int fd) | |||
447 | if (p[idx] == '\n') | 1142 | if (p[idx] == '\n') |
448 | p[idx] = '\0'; | 1143 | p[idx] = '\0'; |
449 | ret = strlist__add(sl, buf); | 1144 | ret = strlist__add(sl, buf); |
450 | if (ret < 0) | 1145 | if (ret < 0) { |
451 | die("strlist__add failed: %s", strerror(-ret)); | 1146 | pr_debug("strlist__add failed: %s\n", strerror(-ret)); |
1147 | strlist__delete(sl); | ||
1148 | return NULL; | ||
1149 | } | ||
452 | } | 1150 | } |
453 | fclose(fp); | 1151 | fclose(fp); |
454 | 1152 | ||
455 | return sl; | 1153 | return sl; |
456 | } | 1154 | } |
457 | 1155 | ||
458 | /* Free and zero clear probe_point */ | ||
459 | static void clear_probe_point(struct probe_point *pp) | ||
460 | { | ||
461 | int i; | ||
462 | |||
463 | if (pp->event) | ||
464 | free(pp->event); | ||
465 | if (pp->group) | ||
466 | free(pp->group); | ||
467 | if (pp->function) | ||
468 | free(pp->function); | ||
469 | if (pp->file) | ||
470 | free(pp->file); | ||
471 | if (pp->lazy_line) | ||
472 | free(pp->lazy_line); | ||
473 | for (i = 0; i < pp->nr_args; i++) | ||
474 | free(pp->args[i]); | ||
475 | if (pp->args) | ||
476 | free(pp->args); | ||
477 | for (i = 0; i < pp->found; i++) | ||
478 | free(pp->probes[i]); | ||
479 | memset(pp, 0, sizeof(*pp)); | ||
480 | } | ||
481 | |||
482 | /* Show an event */ | 1156 | /* Show an event */ |
483 | static void show_perf_probe_event(const char *event, const char *place, | 1157 | static int show_perf_probe_event(struct perf_probe_event *pev) |
484 | struct probe_point *pp) | ||
485 | { | 1158 | { |
486 | int i, ret; | 1159 | int i, ret; |
487 | char buf[128]; | 1160 | char buf[128]; |
1161 | char *place; | ||
1162 | |||
1163 | /* Synthesize only event probe point */ | ||
1164 | place = synthesize_perf_probe_point(&pev->point); | ||
1165 | if (!place) | ||
1166 | return -EINVAL; | ||
488 | 1167 | ||
489 | ret = e_snprintf(buf, 128, "%s:%s", pp->group, event); | 1168 | ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); |
490 | if (ret < 0) | 1169 | if (ret < 0) |
491 | die("Failed to copy event: %s", strerror(-ret)); | 1170 | return ret; |
492 | printf(" %-40s (on %s", buf, place); | 1171 | |
1172 | printf(" %-20s (on %s", buf, place); | ||
493 | 1173 | ||
494 | if (pp->nr_args > 0) { | 1174 | if (pev->nargs > 0) { |
495 | printf(" with"); | 1175 | printf(" with"); |
496 | for (i = 0; i < pp->nr_args; i++) | 1176 | for (i = 0; i < pev->nargs; i++) { |
497 | printf(" %s", pp->args[i]); | 1177 | ret = synthesize_perf_probe_arg(&pev->args[i], |
1178 | buf, 128); | ||
1179 | if (ret < 0) | ||
1180 | break; | ||
1181 | printf(" %s", buf); | ||
1182 | } | ||
498 | } | 1183 | } |
499 | printf(")\n"); | 1184 | printf(")\n"); |
1185 | free(place); | ||
1186 | return ret; | ||
500 | } | 1187 | } |
501 | 1188 | ||
502 | /* List up current perf-probe events */ | 1189 | /* List up current perf-probe events */ |
503 | void show_perf_probe_events(void) | 1190 | int show_perf_probe_events(void) |
504 | { | 1191 | { |
505 | int fd; | 1192 | int fd, ret; |
506 | struct probe_point pp; | 1193 | struct kprobe_trace_event tev; |
1194 | struct perf_probe_event pev; | ||
507 | struct strlist *rawlist; | 1195 | struct strlist *rawlist; |
508 | struct str_node *ent; | 1196 | struct str_node *ent; |
509 | 1197 | ||
510 | setup_pager(); | 1198 | setup_pager(); |
511 | memset(&pp, 0, sizeof(pp)); | 1199 | ret = init_vmlinux(); |
1200 | if (ret < 0) | ||
1201 | return ret; | ||
1202 | |||
1203 | memset(&tev, 0, sizeof(tev)); | ||
1204 | memset(&pev, 0, sizeof(pev)); | ||
512 | 1205 | ||
513 | fd = open_kprobe_events(O_RDONLY, 0); | 1206 | fd = open_kprobe_events(false); |
514 | rawlist = get_trace_kprobe_event_rawlist(fd); | 1207 | if (fd < 0) |
1208 | return fd; | ||
1209 | |||
1210 | rawlist = get_kprobe_trace_command_rawlist(fd); | ||
515 | close(fd); | 1211 | close(fd); |
1212 | if (!rawlist) | ||
1213 | return -ENOENT; | ||
516 | 1214 | ||
517 | strlist__for_each(ent, rawlist) { | 1215 | strlist__for_each(ent, rawlist) { |
518 | parse_trace_kprobe_event(ent->s, &pp); | 1216 | ret = parse_kprobe_trace_command(ent->s, &tev); |
519 | /* Synthesize only event probe point */ | 1217 | if (ret >= 0) { |
520 | synthesize_perf_probe_point(&pp); | 1218 | ret = convert_to_perf_probe_event(&tev, &pev); |
521 | /* Show an event */ | 1219 | if (ret >= 0) |
522 | show_perf_probe_event(pp.event, pp.probes[0], &pp); | 1220 | ret = show_perf_probe_event(&pev); |
523 | clear_probe_point(&pp); | 1221 | } |
1222 | clear_perf_probe_event(&pev); | ||
1223 | clear_kprobe_trace_event(&tev); | ||
1224 | if (ret < 0) | ||
1225 | break; | ||
524 | } | 1226 | } |
525 | |||
526 | strlist__delete(rawlist); | 1227 | strlist__delete(rawlist); |
1228 | |||
1229 | return ret; | ||
527 | } | 1230 | } |
528 | 1231 | ||
529 | /* Get current perf-probe event names */ | 1232 | /* Get current perf-probe event names */ |
530 | static struct strlist *get_perf_event_names(int fd, bool include_group) | 1233 | static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group) |
531 | { | 1234 | { |
532 | char buf[128]; | 1235 | char buf[128]; |
533 | struct strlist *sl, *rawlist; | 1236 | struct strlist *sl, *rawlist; |
534 | struct str_node *ent; | 1237 | struct str_node *ent; |
535 | struct probe_point pp; | 1238 | struct kprobe_trace_event tev; |
1239 | int ret = 0; | ||
536 | 1240 | ||
537 | memset(&pp, 0, sizeof(pp)); | 1241 | memset(&tev, 0, sizeof(tev)); |
538 | rawlist = get_trace_kprobe_event_rawlist(fd); | ||
539 | 1242 | ||
1243 | rawlist = get_kprobe_trace_command_rawlist(fd); | ||
540 | sl = strlist__new(true, NULL); | 1244 | sl = strlist__new(true, NULL); |
541 | strlist__for_each(ent, rawlist) { | 1245 | strlist__for_each(ent, rawlist) { |
542 | parse_trace_kprobe_event(ent->s, &pp); | 1246 | ret = parse_kprobe_trace_command(ent->s, &tev); |
1247 | if (ret < 0) | ||
1248 | break; | ||
543 | if (include_group) { | 1249 | if (include_group) { |
544 | if (e_snprintf(buf, 128, "%s:%s", pp.group, | 1250 | ret = e_snprintf(buf, 128, "%s:%s", tev.group, |
545 | pp.event) < 0) | 1251 | tev.event); |
546 | die("Failed to copy group:event name."); | 1252 | if (ret >= 0) |
547 | strlist__add(sl, buf); | 1253 | ret = strlist__add(sl, buf); |
548 | } else | 1254 | } else |
549 | strlist__add(sl, pp.event); | 1255 | ret = strlist__add(sl, tev.event); |
550 | clear_probe_point(&pp); | 1256 | clear_kprobe_trace_event(&tev); |
1257 | if (ret < 0) | ||
1258 | break; | ||
551 | } | 1259 | } |
552 | |||
553 | strlist__delete(rawlist); | 1260 | strlist__delete(rawlist); |
554 | 1261 | ||
1262 | if (ret < 0) { | ||
1263 | strlist__delete(sl); | ||
1264 | return NULL; | ||
1265 | } | ||
555 | return sl; | 1266 | return sl; |
556 | } | 1267 | } |
557 | 1268 | ||
558 | static void write_trace_kprobe_event(int fd, const char *buf) | 1269 | static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev) |
559 | { | 1270 | { |
560 | int ret; | 1271 | int ret = 0; |
1272 | char *buf = synthesize_kprobe_trace_command(tev); | ||
1273 | |||
1274 | if (!buf) { | ||
1275 | pr_debug("Failed to synthesize kprobe trace event.\n"); | ||
1276 | return -EINVAL; | ||
1277 | } | ||
561 | 1278 | ||
562 | pr_debug("Writing event: %s\n", buf); | 1279 | pr_debug("Writing event: %s\n", buf); |
563 | ret = write(fd, buf, strlen(buf)); | 1280 | if (!probe_event_dry_run) { |
564 | if (ret <= 0) | 1281 | ret = write(fd, buf, strlen(buf)); |
565 | die("Failed to write event: %s", strerror(errno)); | 1282 | if (ret <= 0) |
1283 | pr_warning("Failed to write event: %s\n", | ||
1284 | strerror(errno)); | ||
1285 | } | ||
1286 | free(buf); | ||
1287 | return ret; | ||
566 | } | 1288 | } |
567 | 1289 | ||
568 | static void get_new_event_name(char *buf, size_t len, const char *base, | 1290 | static int get_new_event_name(char *buf, size_t len, const char *base, |
569 | struct strlist *namelist, bool allow_suffix) | 1291 | struct strlist *namelist, bool allow_suffix) |
570 | { | 1292 | { |
571 | int i, ret; | 1293 | int i, ret; |
572 | 1294 | ||
573 | /* Try no suffix */ | 1295 | /* Try no suffix */ |
574 | ret = e_snprintf(buf, len, "%s", base); | 1296 | ret = e_snprintf(buf, len, "%s", base); |
575 | if (ret < 0) | 1297 | if (ret < 0) { |
576 | die("snprintf() failed: %s", strerror(-ret)); | 1298 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
1299 | return ret; | ||
1300 | } | ||
577 | if (!strlist__has_entry(namelist, buf)) | 1301 | if (!strlist__has_entry(namelist, buf)) |
578 | return; | 1302 | return 0; |
579 | 1303 | ||
580 | if (!allow_suffix) { | 1304 | if (!allow_suffix) { |
581 | pr_warning("Error: event \"%s\" already exists. " | 1305 | pr_warning("Error: event \"%s\" already exists. " |
582 | "(Use -f to force duplicates.)\n", base); | 1306 | "(Use -f to force duplicates.)\n", base); |
583 | die("Can't add new event."); | 1307 | return -EEXIST; |
584 | } | 1308 | } |
585 | 1309 | ||
586 | /* Try to add suffix */ | 1310 | /* Try to add suffix */ |
587 | for (i = 1; i < MAX_EVENT_INDEX; i++) { | 1311 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
588 | ret = e_snprintf(buf, len, "%s_%d", base, i); | 1312 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
589 | if (ret < 0) | 1313 | if (ret < 0) { |
590 | die("snprintf() failed: %s", strerror(-ret)); | 1314 | pr_debug("snprintf() failed: %s\n", strerror(-ret)); |
1315 | return ret; | ||
1316 | } | ||
591 | if (!strlist__has_entry(namelist, buf)) | 1317 | if (!strlist__has_entry(namelist, buf)) |
592 | break; | 1318 | break; |
593 | } | 1319 | } |
594 | if (i == MAX_EVENT_INDEX) | 1320 | if (i == MAX_EVENT_INDEX) { |
595 | die("Too many events are on the same function."); | 1321 | pr_warning("Too many events are on the same function.\n"); |
1322 | ret = -ERANGE; | ||
1323 | } | ||
1324 | |||
1325 | return ret; | ||
596 | } | 1326 | } |
597 | 1327 | ||
598 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes, | 1328 | static int __add_kprobe_trace_events(struct perf_probe_event *pev, |
599 | bool force_add) | 1329 | struct kprobe_trace_event *tevs, |
1330 | int ntevs, bool allow_suffix) | ||
600 | { | 1331 | { |
601 | int i, j, fd; | 1332 | int i, fd, ret; |
602 | struct probe_point *pp; | 1333 | struct kprobe_trace_event *tev = NULL; |
603 | char buf[MAX_CMDLEN]; | 1334 | char buf[64]; |
604 | char event[64]; | 1335 | const char *event, *group; |
605 | struct strlist *namelist; | 1336 | struct strlist *namelist; |
606 | bool allow_suffix; | ||
607 | 1337 | ||
608 | fd = open_kprobe_events(O_RDWR, O_APPEND); | 1338 | fd = open_kprobe_events(true); |
1339 | if (fd < 0) | ||
1340 | return fd; | ||
609 | /* Get current event names */ | 1341 | /* Get current event names */ |
610 | namelist = get_perf_event_names(fd, false); | 1342 | namelist = get_kprobe_trace_event_names(fd, false); |
611 | 1343 | if (!namelist) { | |
612 | for (j = 0; j < nr_probes; j++) { | 1344 | pr_debug("Failed to get current event list.\n"); |
613 | pp = probes + j; | 1345 | return -EIO; |
614 | if (!pp->event) | 1346 | } |
615 | pp->event = strdup(pp->function); | 1347 | |
616 | if (!pp->group) | 1348 | ret = 0; |
617 | pp->group = strdup(PERFPROBE_GROUP); | 1349 | printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); |
618 | DIE_IF(!pp->event || !pp->group); | 1350 | for (i = 0; i < ntevs; i++) { |
619 | /* If force_add is true, suffix search is allowed */ | 1351 | tev = &tevs[i]; |
620 | allow_suffix = force_add; | 1352 | if (pev->event) |
621 | for (i = 0; i < pp->found; i++) { | 1353 | event = pev->event; |
622 | /* Get an unused new event name */ | 1354 | else |
623 | get_new_event_name(event, 64, pp->event, namelist, | 1355 | if (pev->point.function) |
624 | allow_suffix); | 1356 | event = pev->point.function; |
625 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n", | 1357 | else |
626 | pp->retprobe ? 'r' : 'p', | 1358 | event = tev->point.symbol; |
627 | pp->group, event, | 1359 | if (pev->group) |
628 | pp->probes[i]); | 1360 | group = pev->group; |
629 | write_trace_kprobe_event(fd, buf); | 1361 | else |
630 | printf("Added new event:\n"); | 1362 | group = PERFPROBE_GROUP; |
631 | /* Get the first parameter (probe-point) */ | 1363 | |
632 | sscanf(pp->probes[i], "%s", buf); | 1364 | /* Get an unused new event name */ |
633 | show_perf_probe_event(event, buf, pp); | 1365 | ret = get_new_event_name(buf, 64, event, |
634 | /* Add added event name to namelist */ | 1366 | namelist, allow_suffix); |
635 | strlist__add(namelist, event); | 1367 | if (ret < 0) |
636 | /* | 1368 | break; |
637 | * Probes after the first probe which comes from same | 1369 | event = buf; |
638 | * user input are always allowed to add suffix, because | 1370 | |
639 | * there might be several addresses corresponding to | 1371 | tev->event = strdup(event); |
640 | * one code line. | 1372 | tev->group = strdup(group); |
641 | */ | 1373 | if (tev->event == NULL || tev->group == NULL) { |
642 | allow_suffix = true; | 1374 | ret = -ENOMEM; |
1375 | break; | ||
643 | } | 1376 | } |
1377 | ret = write_kprobe_trace_event(fd, tev); | ||
1378 | if (ret < 0) | ||
1379 | break; | ||
1380 | /* Add added event name to namelist */ | ||
1381 | strlist__add(namelist, event); | ||
1382 | |||
1383 | /* Trick here - save current event/group */ | ||
1384 | event = pev->event; | ||
1385 | group = pev->group; | ||
1386 | pev->event = tev->event; | ||
1387 | pev->group = tev->group; | ||
1388 | show_perf_probe_event(pev); | ||
1389 | /* Trick here - restore current event/group */ | ||
1390 | pev->event = (char *)event; | ||
1391 | pev->group = (char *)group; | ||
1392 | |||
1393 | /* | ||
1394 | * Probes after the first probe which comes from same | ||
1395 | * user input are always allowed to add suffix, because | ||
1396 | * there might be several addresses corresponding to | ||
1397 | * one code line. | ||
1398 | */ | ||
1399 | allow_suffix = true; | ||
1400 | } | ||
1401 | |||
1402 | if (ret >= 0) { | ||
1403 | /* Show how to use the event. */ | ||
1404 | printf("\nYou can now use it on all perf tools, such as:\n\n"); | ||
1405 | printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, | ||
1406 | tev->event); | ||
644 | } | 1407 | } |
645 | /* Show how to use the event. */ | ||
646 | printf("\nYou can now use it on all perf tools, such as:\n\n"); | ||
647 | printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event); | ||
648 | 1408 | ||
649 | strlist__delete(namelist); | 1409 | strlist__delete(namelist); |
650 | close(fd); | 1410 | close(fd); |
1411 | return ret; | ||
1412 | } | ||
1413 | |||
1414 | static int convert_to_kprobe_trace_events(struct perf_probe_event *pev, | ||
1415 | struct kprobe_trace_event **tevs, | ||
1416 | int max_tevs) | ||
1417 | { | ||
1418 | struct symbol *sym; | ||
1419 | int ret = 0, i; | ||
1420 | struct kprobe_trace_event *tev; | ||
1421 | |||
1422 | /* Convert perf_probe_event with debuginfo */ | ||
1423 | ret = try_to_find_kprobe_trace_events(pev, tevs, max_tevs); | ||
1424 | if (ret != 0) | ||
1425 | return ret; | ||
1426 | |||
1427 | /* Allocate trace event buffer */ | ||
1428 | tev = *tevs = zalloc(sizeof(struct kprobe_trace_event)); | ||
1429 | if (tev == NULL) | ||
1430 | return -ENOMEM; | ||
1431 | |||
1432 | /* Copy parameters */ | ||
1433 | tev->point.symbol = strdup(pev->point.function); | ||
1434 | if (tev->point.symbol == NULL) { | ||
1435 | ret = -ENOMEM; | ||
1436 | goto error; | ||
1437 | } | ||
1438 | tev->point.offset = pev->point.offset; | ||
1439 | tev->nargs = pev->nargs; | ||
1440 | if (tev->nargs) { | ||
1441 | tev->args = zalloc(sizeof(struct kprobe_trace_arg) | ||
1442 | * tev->nargs); | ||
1443 | if (tev->args == NULL) { | ||
1444 | ret = -ENOMEM; | ||
1445 | goto error; | ||
1446 | } | ||
1447 | for (i = 0; i < tev->nargs; i++) { | ||
1448 | if (pev->args[i].name) { | ||
1449 | tev->args[i].name = strdup(pev->args[i].name); | ||
1450 | if (tev->args[i].name == NULL) { | ||
1451 | ret = -ENOMEM; | ||
1452 | goto error; | ||
1453 | } | ||
1454 | } | ||
1455 | tev->args[i].value = strdup(pev->args[i].var); | ||
1456 | if (tev->args[i].value == NULL) { | ||
1457 | ret = -ENOMEM; | ||
1458 | goto error; | ||
1459 | } | ||
1460 | if (pev->args[i].type) { | ||
1461 | tev->args[i].type = strdup(pev->args[i].type); | ||
1462 | if (tev->args[i].type == NULL) { | ||
1463 | ret = -ENOMEM; | ||
1464 | goto error; | ||
1465 | } | ||
1466 | } | ||
1467 | } | ||
1468 | } | ||
1469 | |||
1470 | /* Currently just checking function name from symbol map */ | ||
1471 | sym = map__find_symbol_by_name(machine.vmlinux_maps[MAP__FUNCTION], | ||
1472 | tev->point.symbol, NULL); | ||
1473 | if (!sym) { | ||
1474 | pr_warning("Kernel symbol \'%s\' not found.\n", | ||
1475 | tev->point.symbol); | ||
1476 | ret = -ENOENT; | ||
1477 | goto error; | ||
1478 | } | ||
1479 | |||
1480 | return 1; | ||
1481 | error: | ||
1482 | clear_kprobe_trace_event(tev); | ||
1483 | free(tev); | ||
1484 | *tevs = NULL; | ||
1485 | return ret; | ||
1486 | } | ||
1487 | |||
1488 | struct __event_package { | ||
1489 | struct perf_probe_event *pev; | ||
1490 | struct kprobe_trace_event *tevs; | ||
1491 | int ntevs; | ||
1492 | }; | ||
1493 | |||
1494 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, | ||
1495 | bool force_add, int max_tevs) | ||
1496 | { | ||
1497 | int i, j, ret; | ||
1498 | struct __event_package *pkgs; | ||
1499 | |||
1500 | pkgs = zalloc(sizeof(struct __event_package) * npevs); | ||
1501 | if (pkgs == NULL) | ||
1502 | return -ENOMEM; | ||
1503 | |||
1504 | /* Init vmlinux path */ | ||
1505 | ret = init_vmlinux(); | ||
1506 | if (ret < 0) | ||
1507 | return ret; | ||
1508 | |||
1509 | /* Loop 1: convert all events */ | ||
1510 | for (i = 0; i < npevs; i++) { | ||
1511 | pkgs[i].pev = &pevs[i]; | ||
1512 | /* Convert with or without debuginfo */ | ||
1513 | ret = convert_to_kprobe_trace_events(pkgs[i].pev, | ||
1514 | &pkgs[i].tevs, max_tevs); | ||
1515 | if (ret < 0) | ||
1516 | goto end; | ||
1517 | pkgs[i].ntevs = ret; | ||
1518 | } | ||
1519 | |||
1520 | /* Loop 2: add all events */ | ||
1521 | for (i = 0; i < npevs && ret >= 0; i++) | ||
1522 | ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs, | ||
1523 | pkgs[i].ntevs, force_add); | ||
1524 | end: | ||
1525 | /* Loop 3: cleanup trace events */ | ||
1526 | for (i = 0; i < npevs; i++) | ||
1527 | for (j = 0; j < pkgs[i].ntevs; j++) | ||
1528 | clear_kprobe_trace_event(&pkgs[i].tevs[j]); | ||
1529 | |||
1530 | return ret; | ||
651 | } | 1531 | } |
652 | 1532 | ||
653 | static void __del_trace_kprobe_event(int fd, struct str_node *ent) | 1533 | static int __del_trace_kprobe_event(int fd, struct str_node *ent) |
654 | { | 1534 | { |
655 | char *p; | 1535 | char *p; |
656 | char buf[128]; | 1536 | char buf[128]; |
1537 | int ret; | ||
657 | 1538 | ||
658 | /* Convert from perf-probe event to trace-kprobe event */ | 1539 | /* Convert from perf-probe event to trace-kprobe event */ |
659 | if (e_snprintf(buf, 128, "-:%s", ent->s) < 0) | 1540 | ret = e_snprintf(buf, 128, "-:%s", ent->s); |
660 | die("Failed to copy event."); | 1541 | if (ret < 0) |
1542 | goto error; | ||
1543 | |||
661 | p = strchr(buf + 2, ':'); | 1544 | p = strchr(buf + 2, ':'); |
662 | if (!p) | 1545 | if (!p) { |
663 | die("Internal error: %s should have ':' but not.", ent->s); | 1546 | pr_debug("Internal error: %s should have ':' but not.\n", |
1547 | ent->s); | ||
1548 | ret = -ENOTSUP; | ||
1549 | goto error; | ||
1550 | } | ||
664 | *p = '/'; | 1551 | *p = '/'; |
665 | 1552 | ||
666 | write_trace_kprobe_event(fd, buf); | 1553 | pr_debug("Writing event: %s\n", buf); |
1554 | ret = write(fd, buf, strlen(buf)); | ||
1555 | if (ret < 0) | ||
1556 | goto error; | ||
1557 | |||
667 | printf("Remove event: %s\n", ent->s); | 1558 | printf("Remove event: %s\n", ent->s); |
1559 | return 0; | ||
1560 | error: | ||
1561 | pr_warning("Failed to delete event: %s\n", strerror(-ret)); | ||
1562 | return ret; | ||
668 | } | 1563 | } |
669 | 1564 | ||
670 | static void del_trace_kprobe_event(int fd, const char *group, | 1565 | static int del_trace_kprobe_event(int fd, const char *group, |
671 | const char *event, struct strlist *namelist) | 1566 | const char *event, struct strlist *namelist) |
672 | { | 1567 | { |
673 | char buf[128]; | 1568 | char buf[128]; |
674 | struct str_node *ent, *n; | 1569 | struct str_node *ent, *n; |
675 | int found = 0; | 1570 | int found = 0, ret = 0; |
676 | 1571 | ||
677 | if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) | 1572 | ret = e_snprintf(buf, 128, "%s:%s", group, event); |
678 | die("Failed to copy event."); | 1573 | if (ret < 0) { |
1574 | pr_err("Failed to copy event."); | ||
1575 | return ret; | ||
1576 | } | ||
679 | 1577 | ||
680 | if (strpbrk(buf, "*?")) { /* Glob-exp */ | 1578 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
681 | strlist__for_each_safe(ent, n, namelist) | 1579 | strlist__for_each_safe(ent, n, namelist) |
682 | if (strglobmatch(ent->s, buf)) { | 1580 | if (strglobmatch(ent->s, buf)) { |
683 | found++; | 1581 | found++; |
684 | __del_trace_kprobe_event(fd, ent); | 1582 | ret = __del_trace_kprobe_event(fd, ent); |
1583 | if (ret < 0) | ||
1584 | break; | ||
685 | strlist__remove(namelist, ent); | 1585 | strlist__remove(namelist, ent); |
686 | } | 1586 | } |
687 | } else { | 1587 | } else { |
688 | ent = strlist__find(namelist, buf); | 1588 | ent = strlist__find(namelist, buf); |
689 | if (ent) { | 1589 | if (ent) { |
690 | found++; | 1590 | found++; |
691 | __del_trace_kprobe_event(fd, ent); | 1591 | ret = __del_trace_kprobe_event(fd, ent); |
692 | strlist__remove(namelist, ent); | 1592 | if (ret >= 0) |
1593 | strlist__remove(namelist, ent); | ||
693 | } | 1594 | } |
694 | } | 1595 | } |
695 | if (found == 0) | 1596 | if (found == 0 && ret >= 0) |
696 | pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf); | 1597 | pr_info("Info: Event \"%s\" does not exist.\n", buf); |
1598 | |||
1599 | return ret; | ||
697 | } | 1600 | } |
698 | 1601 | ||
699 | void del_trace_kprobe_events(struct strlist *dellist) | 1602 | int del_perf_probe_events(struct strlist *dellist) |
700 | { | 1603 | { |
701 | int fd; | 1604 | int fd, ret = 0; |
702 | const char *group, *event; | 1605 | const char *group, *event; |
703 | char *p, *str; | 1606 | char *p, *str; |
704 | struct str_node *ent; | 1607 | struct str_node *ent; |
705 | struct strlist *namelist; | 1608 | struct strlist *namelist; |
706 | 1609 | ||
707 | fd = open_kprobe_events(O_RDWR, O_APPEND); | 1610 | fd = open_kprobe_events(true); |
1611 | if (fd < 0) | ||
1612 | return fd; | ||
1613 | |||
708 | /* Get current event names */ | 1614 | /* Get current event names */ |
709 | namelist = get_perf_event_names(fd, true); | 1615 | namelist = get_kprobe_trace_event_names(fd, true); |
1616 | if (namelist == NULL) | ||
1617 | return -EINVAL; | ||
710 | 1618 | ||
711 | strlist__for_each(ent, dellist) { | 1619 | strlist__for_each(ent, dellist) { |
712 | str = strdup(ent->s); | 1620 | str = strdup(ent->s); |
713 | if (!str) | 1621 | if (str == NULL) { |
714 | die("Failed to copy event."); | 1622 | ret = -ENOMEM; |
1623 | break; | ||
1624 | } | ||
715 | pr_debug("Parsing: %s\n", str); | 1625 | pr_debug("Parsing: %s\n", str); |
716 | p = strchr(str, ':'); | 1626 | p = strchr(str, ':'); |
717 | if (p) { | 1627 | if (p) { |
@@ -723,80 +1633,14 @@ void del_trace_kprobe_events(struct strlist *dellist) | |||
723 | event = str; | 1633 | event = str; |
724 | } | 1634 | } |
725 | pr_debug("Group: %s, Event: %s\n", group, event); | 1635 | pr_debug("Group: %s, Event: %s\n", group, event); |
726 | del_trace_kprobe_event(fd, group, event, namelist); | 1636 | ret = del_trace_kprobe_event(fd, group, event, namelist); |
727 | free(str); | 1637 | free(str); |
1638 | if (ret < 0) | ||
1639 | break; | ||
728 | } | 1640 | } |
729 | strlist__delete(namelist); | 1641 | strlist__delete(namelist); |
730 | close(fd); | 1642 | close(fd); |
731 | } | ||
732 | 1643 | ||
733 | #define LINEBUF_SIZE 256 | 1644 | return ret; |
734 | #define NR_ADDITIONAL_LINES 2 | ||
735 | |||
736 | static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num) | ||
737 | { | ||
738 | char buf[LINEBUF_SIZE]; | ||
739 | const char *color = PERF_COLOR_BLUE; | ||
740 | |||
741 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) | ||
742 | goto error; | ||
743 | if (!skip) { | ||
744 | if (show_num) | ||
745 | fprintf(stdout, "%7u %s", l, buf); | ||
746 | else | ||
747 | color_fprintf(stdout, color, " %s", buf); | ||
748 | } | ||
749 | |||
750 | while (strlen(buf) == LINEBUF_SIZE - 1 && | ||
751 | buf[LINEBUF_SIZE - 2] != '\n') { | ||
752 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) | ||
753 | goto error; | ||
754 | if (!skip) { | ||
755 | if (show_num) | ||
756 | fprintf(stdout, "%s", buf); | ||
757 | else | ||
758 | color_fprintf(stdout, color, "%s", buf); | ||
759 | } | ||
760 | } | ||
761 | return; | ||
762 | error: | ||
763 | if (feof(fp)) | ||
764 | die("Source file is shorter than expected."); | ||
765 | else | ||
766 | die("File read error: %s", strerror(errno)); | ||
767 | } | 1645 | } |
768 | 1646 | ||
769 | void show_line_range(struct line_range *lr) | ||
770 | { | ||
771 | unsigned int l = 1; | ||
772 | struct line_node *ln; | ||
773 | FILE *fp; | ||
774 | |||
775 | setup_pager(); | ||
776 | |||
777 | if (lr->function) | ||
778 | fprintf(stdout, "<%s:%d>\n", lr->function, | ||
779 | lr->start - lr->offset); | ||
780 | else | ||
781 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); | ||
782 | |||
783 | fp = fopen(lr->path, "r"); | ||
784 | if (fp == NULL) | ||
785 | die("Failed to open %s: %s", lr->path, strerror(errno)); | ||
786 | /* Skip to starting line number */ | ||
787 | while (l < lr->start) | ||
788 | show_one_line(fp, l++, true, false); | ||
789 | |||
790 | list_for_each_entry(ln, &lr->line_list, list) { | ||
791 | while (ln->line > l) | ||
792 | show_one_line(fp, (l++) - lr->offset, false, false); | ||
793 | show_one_line(fp, (l++) - lr->offset, false, true); | ||
794 | } | ||
795 | |||
796 | if (lr->end == INT_MAX) | ||
797 | lr->end = l + NR_ADDITIONAL_LINES; | ||
798 | while (l < lr->end && !feof(fp)) | ||
799 | show_one_line(fp, (l++) - lr->offset, false, false); | ||
800 | |||
801 | fclose(fp); | ||
802 | } | ||