]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - tools/perf/util/probe-event.c
e42f3acc9a7ee3678b0a8682b1b3f62599e3c0c0
2 * probe-event.c : perf-probe definition to kprobe_events format converter
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
40 #include "parse-events.h" /* For debugfs_path */
41 #include "probe-event.h"
43 #define MAX_CMDLEN 256
44 #define MAX_PROBE_ARGS 128
45 #define PERFPROBE_GROUP "probe"
47 #define semantic_error(msg ...) die("Semantic error :" msg)
49 /* If there is no space to write, returns -E2BIG. */
50 static int e_snprintf(char *str
, size_t size
, const char *format
, ...)
55 ret
= vsnprintf(str
, size
, format
, ap
);
62 /* Parse probepoint definition. */
63 static void parse_perf_probe_probepoint(char *arg
, struct probe_point
*pp
)
70 * perf probe FUNC[+OFFS|%return][@SRC]
73 ptr
= strpbrk(arg
, ":+@%");
79 /* Check arg is function or file and copy it */
80 if (strchr(arg
, '.')) /* File */
81 pp
->file
= strdup(arg
);
83 pp
->function
= strdup(arg
);
84 DIE_IF(pp
->file
== NULL
&& pp
->function
== NULL
);
86 /* Parse other options */
90 ptr
= strpbrk(arg
, ":+@%");
96 case ':': /* Line number */
97 pp
->line
= strtoul(arg
, &tmp
, 0);
99 semantic_error("There is non-digit charactor"
102 case '+': /* Byte offset from a symbol */
103 pp
->offset
= strtoul(arg
, &tmp
, 0);
105 semantic_error("There is non-digit charactor"
108 case '@': /* File name */
110 semantic_error("SRC@SRC is not allowed.");
111 pp
->file
= strdup(arg
);
112 DIE_IF(pp
->file
== NULL
);
114 semantic_error("@SRC must be the last "
117 case '%': /* Probe places */
118 if (strcmp(arg
, "return") == 0) {
120 } else /* Others not supported yet */
121 semantic_error("%%%s is not supported.", arg
);
124 DIE_IF("Program has a bug.");
129 /* Exclusion check */
130 if (pp
->line
&& pp
->offset
)
131 semantic_error("Offset can't be used with line number.");
133 if (!pp
->line
&& pp
->file
&& !pp
->function
)
134 semantic_error("File always requires line number.");
136 if (pp
->offset
&& !pp
->function
)
137 semantic_error("Offset requires an entry function.");
139 if (pp
->retprobe
&& !pp
->function
)
140 semantic_error("Return probe requires an entry function.");
142 if ((pp
->offset
|| pp
->line
) && pp
->retprobe
)
143 semantic_error("Offset/Line can't be used with return probe.");
145 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
146 pp
->function
, pp
->file
, pp
->line
, pp
->offset
, pp
->retprobe
);
149 /* Parse perf-probe event definition */
150 int parse_perf_probe_event(const char *str
, struct probe_point
*pp
)
153 int argc
, i
, need_dwarf
= 0;
155 argv
= argv_split(str
, &argc
);
157 die("argv_split failed.");
158 if (argc
> MAX_PROBE_ARGS
+ 1)
159 semantic_error("Too many arguments");
161 /* Parse probe point */
162 parse_perf_probe_probepoint(argv
[0], pp
);
163 if (pp
->file
|| pp
->line
)
166 /* Copy arguments and ensure return probe has no C argument */
167 pp
->nr_args
= argc
- 1;
168 pp
->args
= zalloc(sizeof(char *) * pp
->nr_args
);
169 for (i
= 0; i
< pp
->nr_args
; i
++) {
170 pp
->args
[i
] = strdup(argv
[i
+ 1]);
172 die("Failed to copy argument.");
173 if (is_c_varname(pp
->args
[i
])) {
175 semantic_error("You can't specify local"
176 " variable for kretprobe");
185 /* Parse kprobe_events event into struct probe_point */
186 void parse_trace_kprobe_event(const char *str
, char **group
, char **event
,
187 struct probe_point
*pp
)
194 pr_debug("Parsing kprobe_events: %s\n", str
);
195 argv
= argv_split(str
, &argc
);
197 die("argv_split failed.");
199 semantic_error("Too less arguments.");
201 /* Scan event and group name. */
202 ret
= sscanf(argv
[0], "%c:%m[^/ \t]/%m[^ \t]",
205 semantic_error("Failed to parse event name: %s", argv
[0]);
206 pr_debug("Group:%s Event:%s probe:%c\n", *group
, *event
, pr
);
211 pp
->retprobe
= (pr
== 'r');
213 /* Scan function name and offset */
214 ret
= sscanf(argv
[1], "%m[^+]+%d", &pp
->function
, &pp
->offset
);
218 /* kprobe_events doesn't have this information */
222 pp
->nr_args
= argc
- 2;
223 pp
->args
= zalloc(sizeof(char *) * pp
->nr_args
);
224 for (i
= 0; i
< pp
->nr_args
; i
++) {
225 p
= strchr(argv
[i
+ 2], '=');
226 if (p
) /* We don't need which register is assigned. */
228 pp
->args
[i
] = strdup(argv
[i
+ 2]);
230 die("Failed to copy argument.");
237 int synthesize_perf_probe_event(struct probe_point
*pp
)
240 char offs
[64] = "", line
[64] = "";
243 pp
->probes
[0] = buf
= zalloc(MAX_CMDLEN
);
245 die("Failed to allocate memory by zalloc.");
247 ret
= e_snprintf(offs
, 64, "+%d", pp
->offset
);
252 ret
= e_snprintf(line
, 64, ":%d", pp
->line
);
258 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s", pp
->function
,
259 offs
, pp
->retprobe
? "%return" : "", line
);
261 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s%s%s%s", pp
->file
, line
);
266 for (i
= 0; i
< pp
->nr_args
; i
++) {
267 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
282 int synthesize_trace_kprobe_event(struct probe_point
*pp
)
287 pp
->probes
[0] = buf
= zalloc(MAX_CMDLEN
);
289 die("Failed to allocate memory by zalloc.");
290 ret
= e_snprintf(buf
, MAX_CMDLEN
, "%s+%d", pp
->function
, pp
->offset
);
295 for (i
= 0; i
< pp
->nr_args
; i
++) {
296 ret
= e_snprintf(&buf
[len
], MAX_CMDLEN
- len
, " %s",
311 static int open_kprobe_events(int flags
, int mode
)
316 ret
= e_snprintf(buf
, PATH_MAX
, "%s/../kprobe_events", debugfs_path
);
318 die("Failed to make kprobe_events path.");
320 ret
= open(buf
, flags
, mode
);
323 die("kprobe_events file does not exist -"
324 " please rebuild with CONFIG_KPROBE_TRACER.");
326 die("Could not open kprobe_events file: %s",
332 /* Get raw string list of current kprobe_events */
333 static struct strlist
*get_trace_kprobe_event_rawlist(int fd
)
337 char buf
[MAX_CMDLEN
];
341 sl
= strlist__new(true, NULL
);
343 fp
= fdopen(dup(fd
), "r");
345 p
= fgets(buf
, MAX_CMDLEN
, fp
);
352 ret
= strlist__add(sl
, buf
);
354 die("strlist__add failed: %s", strerror(-ret
));
361 /* Free and zero clear probe_point */
362 static void clear_probe_point(struct probe_point
*pp
)
370 for (i
= 0; i
< pp
->nr_args
; i
++)
374 for (i
= 0; i
< pp
->found
; i
++)
376 memset(pp
, 0, sizeof(pp
));
379 /* List up current perf-probe events */
380 void show_perf_probe_events(void)
385 struct probe_point pp
;
386 struct strlist
*rawlist
;
387 struct str_node
*ent
;
389 fd
= open_kprobe_events(O_RDONLY
, 0);
390 rawlist
= get_trace_kprobe_event_rawlist(fd
);
393 for (i
= 0; i
< strlist__nr_entries(rawlist
); i
++) {
394 ent
= strlist__entry(rawlist
, i
);
395 parse_trace_kprobe_event(ent
->s
, &group
, &event
, &pp
);
396 synthesize_perf_probe_event(&pp
);
397 printf("[%s:%s]\t%s\n", group
, event
, pp
.probes
[0]);
400 clear_probe_point(&pp
);
403 strlist__delete(rawlist
);
406 /* Get current perf-probe event names */
407 static struct strlist
*get_perf_event_names(int fd
)
411 struct strlist
*sl
, *rawlist
;
412 struct str_node
*ent
;
414 rawlist
= get_trace_kprobe_event_rawlist(fd
);
416 sl
= strlist__new(false, NULL
);
417 for (i
= 0; i
< strlist__nr_entries(rawlist
); i
++) {
418 ent
= strlist__entry(rawlist
, i
);
419 parse_trace_kprobe_event(ent
->s
, &group
, &event
, NULL
);
420 strlist__add(sl
, event
);
424 strlist__delete(rawlist
);
429 static int write_trace_kprobe_event(int fd
, const char *buf
)
433 ret
= write(fd
, buf
, strlen(buf
));
435 die("Failed to create event.");
437 printf("Added new event: %s\n", buf
);
442 static void get_new_event_name(char *buf
, size_t len
, const char *base
,
443 struct strlist
*namelist
)
446 for (i
= 0; i
< MAX_EVENT_INDEX
; i
++) {
447 ret
= e_snprintf(buf
, len
, "%s_%d", base
, i
);
449 die("snprintf() failed: %s", strerror(-ret
));
450 if (!strlist__has_entry(namelist
, buf
))
453 if (i
== MAX_EVENT_INDEX
)
454 die("Too many events are on the same function.");
457 void add_trace_kprobe_events(struct probe_point
*probes
, int nr_probes
)
460 struct probe_point
*pp
;
461 char buf
[MAX_CMDLEN
];
463 struct strlist
*namelist
;
465 fd
= open_kprobe_events(O_RDWR
, O_APPEND
);
466 /* Get current event names */
467 namelist
= get_perf_event_names(fd
);
469 for (j
= 0; j
< nr_probes
; j
++) {
471 for (i
= 0; i
< pp
->found
; i
++) {
472 /* Get an unused new event name */
473 get_new_event_name(event
, 64, pp
->function
, namelist
);
474 snprintf(buf
, MAX_CMDLEN
, "%c:%s/%s %s\n",
475 pp
->retprobe
? 'r' : 'p',
476 PERFPROBE_GROUP
, event
,
478 write_trace_kprobe_event(fd
, buf
);
479 /* Add added event name to namelist */
480 strlist__add(namelist
, event
);