Skip to content

Commit 42bba26

Browse files
mhiramatacmel
authored andcommitted
perf probe: Allow wildcard for cached events
Allo glob wildcard for reusing cached/SDT events. E.g. # perf probe -x /usr/lib64/libc-2.20.so -a %sdt_libc:\* This example adds probes for all SDT in libc. Note that the SDTs must have been scanned by perf buildid-cache. Committer note: Using it to check what of those SDT probes would take place when doing a cargo run (rust): # trace --no-sys --event sdt_libc:* cargo run 0.000 sdt_libc:setjmp:(7f326b69c4d1)) 28.423 sdt_libc:setjmp:(7f4b0a5364d1)) 29.000 sdt_libc:setjmp:(7f4b0a5364d1)) 88.597 sdt_libc:setjmp:(7fc01fd414d1)) 89.220 sdt_libc:setjmp:(7fc01fd414d1)) 95.501 sdt_libc:setjmp:(7f326b69c4d1)) Running `target/debug/hello_world` 97.110 sdt_libc:setjmp:(7f95e09234d1)) Hello, world! # Signed-off-by: Masami Hiramatsu <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Ananth N Mavinakayanahalli <[email protected]> Cc: Brendan Gregg <[email protected]> Cc: Hemant Kumar <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/146831791813.17065.17846564230840594888.stgit@devbox Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 05bf2c8 commit 42bba26

File tree

3 files changed

+138
-10
lines changed

3 files changed

+138
-10
lines changed

tools/perf/util/probe-event.c

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,15 +1204,15 @@ static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
12041204
ptr = strchr(*arg, ':');
12051205
if (ptr) {
12061206
*ptr = '\0';
1207-
if (!is_c_func_name(*arg))
1207+
if (!pev->sdt && !is_c_func_name(*arg))
12081208
goto ng_name;
12091209
pev->group = strdup(*arg);
12101210
if (!pev->group)
12111211
return -ENOMEM;
12121212
*arg = ptr + 1;
12131213
} else
12141214
pev->group = NULL;
1215-
if (!is_c_func_name(*arg)) {
1215+
if (!pev->sdt && !is_c_func_name(*arg)) {
12161216
ng_name:
12171217
semantic_error("%s is bad for event name -it must "
12181218
"follow C symbol-naming rule.\n", *arg);
@@ -1644,6 +1644,7 @@ int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
16441644
ret = -ENOMEM;
16451645
goto out;
16461646
}
1647+
tev->uprobes = (tp->module[0] == '/');
16471648
p++;
16481649
} else
16491650
p = argv[1];
@@ -2518,7 +2519,7 @@ static int probe_trace_event__set_name(struct probe_trace_event *tev,
25182519
int ret;
25192520

25202521
/* If probe_event or trace_event already have the name, reuse it */
2521-
if (pev->event)
2522+
if (pev->event && !pev->sdt)
25222523
event = pev->event;
25232524
else if (tev->event)
25242525
event = tev->event;
@@ -2531,7 +2532,7 @@ static int probe_trace_event__set_name(struct probe_trace_event *tev,
25312532
else
25322533
event = tev->point.realname;
25332534
}
2534-
if (pev->group)
2535+
if (pev->group && !pev->sdt)
25352536
group = pev->group;
25362537
else if (tev->group)
25372538
group = tev->group;
@@ -2894,6 +2895,100 @@ static int try_to_find_absolute_address(struct perf_probe_event *pev,
28942895

28952896
bool __weak arch__prefers_symtab(void) { return false; }
28962897

2898+
/* Concatinate two arrays */
2899+
static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2900+
{
2901+
void *ret;
2902+
2903+
ret = malloc(sz_a + sz_b);
2904+
if (ret) {
2905+
memcpy(ret, a, sz_a);
2906+
memcpy(ret + sz_a, b, sz_b);
2907+
}
2908+
return ret;
2909+
}
2910+
2911+
static int
2912+
concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2913+
struct probe_trace_event **tevs2, int ntevs2)
2914+
{
2915+
struct probe_trace_event *new_tevs;
2916+
int ret = 0;
2917+
2918+
if (ntevs == 0) {
2919+
*tevs = *tevs2;
2920+
*ntevs = ntevs2;
2921+
*tevs2 = NULL;
2922+
return 0;
2923+
}
2924+
2925+
if (*ntevs + ntevs2 > probe_conf.max_probes)
2926+
ret = -E2BIG;
2927+
else {
2928+
/* Concatinate the array of probe_trace_event */
2929+
new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
2930+
*tevs2, ntevs2 * sizeof(**tevs2));
2931+
if (!new_tevs)
2932+
ret = -ENOMEM;
2933+
else {
2934+
free(*tevs);
2935+
*tevs = new_tevs;
2936+
*ntevs += ntevs2;
2937+
}
2938+
}
2939+
if (ret < 0)
2940+
clear_probe_trace_events(*tevs2, ntevs2);
2941+
zfree(tevs2);
2942+
2943+
return ret;
2944+
}
2945+
2946+
/*
2947+
* Try to find probe_trace_event from given probe caches. Return the number
2948+
* of cached events found, if an error occurs return the error.
2949+
*/
2950+
static int find_cached_events(struct perf_probe_event *pev,
2951+
struct probe_trace_event **tevs,
2952+
const char *target)
2953+
{
2954+
struct probe_cache *cache;
2955+
struct probe_cache_entry *entry;
2956+
struct probe_trace_event *tmp_tevs = NULL;
2957+
int ntevs = 0;
2958+
int ret = 0;
2959+
2960+
cache = probe_cache__new(target);
2961+
/* Return 0 ("not found") if the target has no probe cache. */
2962+
if (!cache)
2963+
return 0;
2964+
2965+
for_each_probe_cache_entry(entry, cache) {
2966+
/* Skip the cache entry which has no name */
2967+
if (!entry->pev.event || !entry->pev.group)
2968+
continue;
2969+
if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
2970+
strglobmatch(entry->pev.event, pev->event)) {
2971+
ret = probe_cache_entry__get_event(entry, &tmp_tevs);
2972+
if (ret > 0)
2973+
ret = concat_probe_trace_events(tevs, &ntevs,
2974+
&tmp_tevs, ret);
2975+
if (ret < 0)
2976+
break;
2977+
}
2978+
}
2979+
probe_cache__delete(cache);
2980+
if (ret < 0) {
2981+
clear_probe_trace_events(*tevs, ntevs);
2982+
zfree(tevs);
2983+
} else {
2984+
ret = ntevs;
2985+
if (ntevs > 0 && target && target[0] == '/')
2986+
pev->uprobes = true;
2987+
}
2988+
2989+
return ret;
2990+
}
2991+
28972992
static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
28982993
struct probe_trace_event **tevs)
28992994
{
@@ -2903,6 +2998,10 @@ static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
29032998
struct str_node *node;
29042999
int ret, i;
29053000

3001+
if (pev->sdt)
3002+
/* For SDT/cached events, we use special search functions */
3003+
return find_cached_events(pev, tevs, pev->target);
3004+
29063005
cache = probe_cache__new(pev->target);
29073006
if (!cache)
29083007
return 0;

tools/perf/util/probe-file.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,38 @@ probe_cache_entry__new(struct perf_probe_event *pev)
362362
return entry;
363363
}
364364

365-
/* For the kernel probe caches, pass target = NULL */
365+
int probe_cache_entry__get_event(struct probe_cache_entry *entry,
366+
struct probe_trace_event **tevs)
367+
{
368+
struct probe_trace_event *tev;
369+
struct str_node *node;
370+
int ret, i;
371+
372+
ret = strlist__nr_entries(entry->tevlist);
373+
if (ret > probe_conf.max_probes)
374+
return -E2BIG;
375+
376+
*tevs = zalloc(ret * sizeof(*tev));
377+
if (!*tevs)
378+
return -ENOMEM;
379+
380+
i = 0;
381+
strlist__for_each_entry(node, entry->tevlist) {
382+
tev = &(*tevs)[i++];
383+
ret = parse_probe_trace_command(node->s, tev);
384+
if (ret < 0)
385+
break;
386+
}
387+
return i;
388+
}
389+
390+
/* For the kernel probe caches, pass target = NULL or DSO__NAME_KALLSYMS */
366391
static int probe_cache__open(struct probe_cache *pcache, const char *target)
367392
{
368393
char cpath[PATH_MAX];
369394
char sbuildid[SBUILD_ID_SIZE];
370395
char *dir_name = NULL;
371-
bool is_kallsyms = !target;
396+
bool is_kallsyms = false;
372397
int ret, fd;
373398

374399
if (target && build_id_cache__cached(target)) {
@@ -378,12 +403,13 @@ static int probe_cache__open(struct probe_cache *pcache, const char *target)
378403
goto found;
379404
}
380405

381-
if (target)
382-
ret = filename__sprintf_build_id(target, sbuildid);
383-
else {
406+
if (!target || !strcmp(target, DSO__NAME_KALLSYMS)) {
384407
target = DSO__NAME_KALLSYMS;
408+
is_kallsyms = true;
385409
ret = sysfs__sprintf_build_id("/", sbuildid);
386-
}
410+
} else
411+
ret = filename__sprintf_build_id(target, sbuildid);
412+
387413
if (ret < 0) {
388414
pr_debug("Failed to get build-id from %s.\n", target);
389415
return ret;

tools/perf/util/probe-file.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ int probe_file__get_events(int fd, struct strfilter *filter,
3434
struct strlist *plist);
3535
int probe_file__del_strlist(int fd, struct strlist *namelist);
3636

37+
int probe_cache_entry__get_event(struct probe_cache_entry *entry,
38+
struct probe_trace_event **tevs);
39+
3740
struct probe_cache *probe_cache__new(const char *target);
3841
int probe_cache__add_entry(struct probe_cache *pcache,
3942
struct perf_probe_event *pev,

0 commit comments

Comments
 (0)