perf-probe --cache support
cover perf-probe --cache support
Hi,
Here is a patches for probe-cache which is going to be perf-cache finally, but I'd like to implement prototype on perf-probe. So, this version is still not ready to be merged.
The perf-probe is useful for debugging, but it strongly depends on the debuginfo. Without debuginfo, it is just a frontend of ftrace's dynamic events. This can usually happen in server farms or on cloud system, since no one wants to distribute big debuginfo packages.
To solve this issue, I had tried to make a pre-analyzed probes ( https://lkml.org/lkml/2014/10/31/207 ) but it has a problm that we can't ensure the probed binary is same as what we analyzed. Arnaldo gave me an idea to reuse build-id cache for that perpose and this series is the first prototype of that.
At the same time, Hemant has started to support SDT probes which also use the cache file of SDT info. So I decided to merge this into the same build-id cache. In this version, I couldn't get it done... it still needs a bit more patches. I'll do that in the next version.
In this version, perf probe supports --cache option which means that perf probe manipulate probe caches, for example,
perf probe --cache --add "probe-desc"
does not only add probe events but also add "probe-desc" and it's result on the cache. (Note that the cached entry is always referred even without --cache)
The --list and --del commands also support --cache. Note that both are only manipulate caches, not real events.
Thank you,
---
Masami Hiramatsu (13):
perf-buildid-cache: Use path/to/bin/buildid/elf instead of path/to/bin/buildid perf probe: Simplify __add_probe_trace_events code
perf probe: Move ftrace probe-event operations to probe-file.c perf buildid: Use SBUILD_ID_SIZE macro
perf buildid: Introduce sysfs/filename__sprintf_build_id
perf: Add lsdir to read a directory
perf-buildid-cache: Use lsdir for looking up buildid caches
perf probe: Check kprobe blacklist in converting phase
perf probe: Use strbuf for making strings in probe-event.c
perf probe: Add --cache option to cache the probe definitions perf probe: Use cache entry if possible
perf probe: Show all cached probes
perf probe: Remove caches when --cache is given
tools/perf/builtin-buildid-cache.c | 22 - tools/perf/builtin-buildid-list.c | 28 - tools/perf/builtin-probe.c | 3 tools/perf/util/Build | 1 tools/perf/util/build-id.c | 203 ++++++-- tools/perf/util/build-id.h | 11 tools/perf/util/dso.h | 5 tools/perf/util/probe-event.c | 927 +++++++++++++++--------------------- tools/perf/util/probe-event.h | 15 + tools/perf/util/probe-file.c | 694 +++++++++++++++++++++++++++ tools/perf/util/probe-file.h | 44 ++ tools/perf/util/probe-finder.c | 10 tools/perf/util/symbol.c | 2 tools/perf/util/util.c | 34 + tools/perf/util/util.h | 4 15 files changed, 1386 insertions(+), 617 deletions(-) create mode 100644 tools/perf/util/probe-file.c create mode 100644 tools/perf/util/probe-file.h
Signature · 5 lines
Masami HIRAMATSU Linux Technology Research Center, System Productivity Research Dept. Center for Technology Innovation - Systems Engineering Hitachi, Ltd., Research & Development Group E-mail: [email protected]
RFC PATCH 1/13 perf-buildid-cache: Use path/to/bin/buildid/elf instead of path/to/bin/buildid
Use path/to/bin/buildid/elf instead of path/to/bin/buildid to store corresponding elf binary.
This also stores vdso in buildid/vdso, kallsyms in buildid/kallsyms. Note that the build-id based symlink changes to point to the path/to/bin/buildid, not path/to/bin/buildid/elf.
--- tools/perf/util/build-id.c | 61 ++++++++++++++++++++++++++++++++------------ tools/perf/util/dso.h | 5 ++++ tools/perf/util/symbol.c | 2 + 3 files changed, 50 insertions(+), 18 deletions(-)
tools/perf/util/build-id.c +44 -17
@@ -112,7 +112,8 @@ static int asnprintf(char **strp, size_t size, const char *fmt, ...)return ret; } -static char *build_id__filename (const char *sbuild_id,char *bf,size_t size) +static char *build_id_cache__linkname (const char *sbuild_id,char *bf, +size_t size) {char *tmp = bf; int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
@@ -122,15 +123,35 @@ static char *build_id__filename(const char *sbuild_id, char *bf, size_t size)return bf; } +static const char *build_id_cache__basename (bool is_kallsyms,bool is_vdso) +{ +return is_kallsyms ?"kallsyms" : (is_vdso ?"vdso" :"elf" ); +} +char *dso__build_id_filename (const struct dso *dso,char *bf,size_t size) { +bool is_kallsyms =dso__is_kallsyms ((struct dso *)dso); +bool is_vdso =dso__is_vdso ((struct dso *)dso);char build_id_hex[BUILD_ID_SIZE *2 +1 ]; +char *linkname; +bool alloc = (bf ==NULL ); +int ret;if (!dso->has_build_id)return NULL ;build_id__sprintf (dso->build_id,sizeof (dso->build_id), build_id_hex); -return build_id__filename (build_id_hex, bf, size); + linkname =build_id_cache__linkname (build_id_hex,NULL ,0 ); +if (!linkname) +return NULL ; + + ret =asnprintf (&bf, size,"%s/%s" , linkname, +build_id_cache__basename (is_kallsyms, is_vdso)); +if (ret <0 || (!alloc && size < (unsigned int )ret)) + bf =NULL ; +free (linkname); + +return bf; }#define dsos__for_each_with_build_id (pos, head) \
@@ -261,7 +282,8 @@ void disable_buildid_cache(void) }static char *build_id_cache__dirname_from_path (const char *name, -bool is_kallsyms,bool is_vdso) +bool is_kallsyms,bool is_vdso, +const char *sbuild_id) {char *realname = (char *)name, *filename;bool slash = is_kallsyms || is_vdso;
@@ -272,8 +294,9 @@ static char *build_id_cache__dirname_from_path(const char *name,return NULL ; } -if (asprintf (&filename,"%s%s%s" , buildid_dir, slash ?"/" :"" , - is_vdso ?DSO__NAME_VDSO : realname) <0 ) +if (asprintf (&filename,"%s%s%s%s%s" , buildid_dir, slash ?"/" :"" , + is_vdso ?DSO__NAME_VDSO : realname, + sbuild_id ?"/" :"" , sbuild_id ?:"" ) <0 ) filename =NULL ;if (!slash)
@@ -292,7 +315,8 @@ int build_id_cache__list_build_ids(const char *pathname,int ret =0 ; list =strlist__new (true,NULL ); - dir_name =build_id_cache__dirname_from_path (pathname, false, false); + dir_name =build_id_cache__dirname_from_path (pathname, false, false, +NULL );if (!list || !dir_name) { ret = -ENOMEM ; goto out;
@@ -327,7 +351,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name, {const size_t size =PATH_MAX ;char *realname =NULL , *filename =NULL , *dir_name =NULL , - *linkname =zalloc (size), *targetname, *tmp; + *linkname =zalloc (size), *tmp;int err =-1 ;if (!is_kallsyms) {
@@ -336,14 +360,17 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name, goto out_free; } - dir_name =build_id_cache__dirname_from_path (name, is_kallsyms, is_vdso); + dir_name =build_id_cache__dirname_from_path (name, is_kallsyms, + is_vdso, sbuild_id);if (!dir_name) goto out_free;if (mkdir_p (dir_name,0755 )) goto out_free; -if (asprintf (&filename,"%s/%s" , dir_name, sbuild_id) <0 ) { +/* Save the allocated buildid dirname */ +if (asprintf (&filename,"%s/%s" , dir_name, +build_id_cache__basename (is_kallsyms, is_vdso)) <0 ) { filename =NULL ; goto out_free; }
@@ -357,7 +384,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name, goto out_free; } -if (!build_id__filename (sbuild_id, linkname, size)) +if (!build_id_cache__linkname (sbuild_id, linkname, size)) goto out_free; tmp =strrchr (linkname,'/' ); *tmp ='\0' ;
@@ -366,10 +393,10 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name, goto out_free; *tmp ='/' ; - targetname = filename +strlen (buildid_dir) -5 ; -memcpy (targetname,"../.." ,5 ); + tmp = dir_name +strlen (buildid_dir) -5 ; +memcpy (tmp,"../.." ,5 ); -if (symlink (targetname, linkname) ==0 ) +if (symlink (tmp, linkname) ==0 ) err =0 ; out_free:if (!is_kallsyms)
@@ -394,7 +421,7 @@ static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,bool build_id_cache__cached (const char *sbuild_id) {bool ret = false; -char *filename =build_id__filename (sbuild_id,NULL ,0 ); +char *filename =build_id_cache__linkname (sbuild_id,NULL ,0 );if (filename && !access (filename,F_OK )) ret = true;
@@ -413,7 +440,7 @@ int build_id_cache__remove_s(const char *sbuild_id)if (filename ==NULL || linkname ==NULL ) goto out_free; -if (!build_id__filename (sbuild_id, linkname, size)) +if (!build_id_cache__linkname (sbuild_id, linkname, size)) goto out_free;if (access (linkname,F_OK ))
@@ -431,7 +458,7 @@ int build_id_cache__remove_s(const char *sbuild_id) tmp =strrchr (linkname,'/' ) +1 ;snprintf (tmp, size - (tmp - linkname),"%s" , filename); -if (unlink (linkname)) +if (rm_rf (linkname)) goto out_free; err =0 ;
@@ -443,7 +470,7 @@ out_free:static int dso__cache_build_id (struct dso *dso,struct machine *machine) { -bool is_kallsyms = dso->kernel && dso->long_name[0 ] !='/' ; +bool is_kallsyms =dso__is_kallsyms (dso);bool is_vdso =dso__is_vdso (dso);const char *name = dso->long_name;char nm[PATH_MAX ];
tools/perf/util/dso.h +5 -0
@@ -325,6 +325,11 @@ static inline bool dso__is_kcore(struct dso *dso) dso->binary_type ==DSO_BINARY_TYPE__GUEST_KCORE ; } +static inline bool dso__is_kallsyms (struct dso *dso) +{ +return dso->kernel && dso->long_name[0 ] !='/' ; +} +void dso__free_a2l (struct dso *dso);enum dso_type dso__type (struct dso *dso,struct machine *machine);
tools/perf/util/symbol.c +1 -1
@@ -1699,7 +1699,7 @@ static char *dso__find_kallsyms(struct dso *dso, struct map *map)if (!find_matching_kcore (map, path,sizeof (path)))return strdup (path); -scnprintf (path,sizeof (path),"%s/[kernel.kallsyms]/%s" , +scnprintf (path,sizeof (path),"%s/[kernel.kallsyms]/%s/kallsyms" , buildid_dir, sbuild_id);if (access (path,F_OK )) {
RFC PATCH 2/13 perf probe: Simplify __add_probe_trace_events code
In reply to Masami HiramatsuSimplify the __add_probe_trace_events() code by taking out the probe_trace_event__set_name() and updating show_perf_probe_event()
--- tools/perf/util/probe-event.c | 92 +++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 41 deletions(-)
tools/perf/util/probe-event.c +51 -41
@@ -2127,7 +2127,8 @@ kprobe_blacklist__find_by_address(struct list_head *blacklist, }/* Show an event */ -static int show_perf_probe_event (struct perf_probe_event *pev, +static int show_perf_probe_event (const char *group,const char *event, +struct perf_probe_event *pev,const char *module) {int i, ret;
@@ -2139,7 +2140,7 @@ static int show_perf_probe_event(struct perf_probe_event *pev,if (!place)return -EINVAL ; - ret =e_snprintf (buf,128 ,"%s:%s" , pev->group, pev->event); + ret =e_snprintf (buf,128 ,"%s:%s" , group, event);if (ret <0 )return ret;
@@ -2201,7 +2202,8 @@ static int __show_perf_probe_events(int fd, bool is_kprobe, ret =convert_to_perf_probe_event (&tev, &pev, is_kprobe);if (ret >=0 ) - ret =show_perf_probe_event (&pev, + ret =show_perf_probe_event (pev.group, + pev.event, &pev, tev.point.module); } next:
@@ -2374,18 +2376,55 @@ out:free (buf); } +/* Set new name from original perf_probe_event and namelist */ +static int probe_trace_event__set_name (struct probe_trace_event *tev, +struct perf_probe_event *pev, +struct strlist *namelist, +bool allow_suffix) +{ +const char *event, *group; +char buf[64 ]; +int ret; + +if (pev->event) + event = pev->event; +else +if (pev->point.function && !strisglob (pev->point.function)) + event = pev->point.function; +else + event = tev->point.realname; +if (pev->group) + group = pev->group; +else + group =PERFPROBE_GROUP ; + +/* Get an unused new event name */ + ret =get_new_event_name (buf,64 , event, + namelist, allow_suffix); +if (ret <0 ) +return ret; + + event = buf; + + tev->event =strdup (event); + tev->group =strdup (group); +if (tev->event ==NULL || tev->group ==NULL ) +return -ENOMEM ; + +/* Add added event name to namelist */ +strlist__add (namelist, event); +return 0 ; +} +static int __add_probe_trace_events (struct perf_probe_event *pev,struct probe_trace_event *tevs,int ntevs,bool allow_suffix) {int i, fd, ret;struct probe_trace_event *tev =NULL ; -char buf[64 ]; -const char *event, *group;struct strlist *namelist;LIST_HEAD (blacklist);struct kprobe_blacklist_node *node; -bool safename;if (pev->uprobes) fd =open_uprobe_events (true);
@@ -2411,7 +2450,6 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,pr_debug ("No kprobe blacklist support, ignored\n" ); } - safename = (pev->point.function && !strisglob (pev->point.function)); ret =0 ;pr_info ("Added new event%s\n" , (ntevs >1 ) ?"s:" :":" );for (i =0 ; i < ntevs; i++) {
@@ -2427,47 +2465,19 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,continue ; } -if (pev->event) - event = pev->event; -else -if (safename) - event = pev->point.function; -else - event = tev->point.realname; -if (pev->group) - group = pev->group; -else - group =PERFPROBE_GROUP ; - -/* Get an unused new event name */ - ret =get_new_event_name (buf,64 , event, - namelist, allow_suffix); +/* Set new name for tev (and update namelist) */ + ret =probe_trace_event__set_name (tev, pev, namelist, + allow_suffix);if (ret <0 )break ; - event = buf; - tev->event =strdup (event); - tev->group =strdup (group); -if (tev->event ==NULL || tev->group ==NULL ) { - ret = -ENOMEM ; -break ; - } ret =write_probe_trace_event (fd, tev);if (ret <0 )break ; -/* Add added event name to namelist */ -strlist__add (namelist, event); - -/* Trick here - save current event/group */ - event = pev->event; - group = pev->group; - pev->event = tev->event; - pev->group = tev->group; -show_perf_probe_event (pev, tev->point.module); -/* Trick here - restore current event/group */ - pev->event = (char *)event; - pev->group = (char *)group; +/* We use tev's name for showing new events */ +show_perf_probe_event (tev->group, tev->event, pev, + tev->point.module);/* * Probes after the first probe which comes from same * user input are always allowed to add suffix, because
RFC PATCH 3/13 perf probe: Move ftrace probe-event operations to probe-file.c
In reply to Masami HiramatsuMove ftrace probe-event operations to probe-file.c from probe-event.c.
--- tools/perf/util/Build | 1 tools/perf/util/probe-event.c | 319 +++-------------------------------------- tools/perf/util/probe-event.h | 7 + tools/perf/util/probe-file.c | 302 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/probe-file.h | 18 ++ 5 files changed, 355 insertions(+), 292 deletions(-) create mode 100644 tools/perf/util/probe-file.c create mode 100644 tools/perf/util/probe-file.h
tools/perf/util/Build +1 -0
@@ -78,6 +78,7 @@ libperf-$(CONFIG_AUXTRACE) += auxtrace.o libperf-y += parse-branch-options.o libperf-$(CONFIG_LIBELF) += symbol-elf.o +libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.o ifndef CONFIG_LIBELF
tools/perf/util/probe-event.c +27 -292
419 lines not on this page · raw message
tools/perf/util/probe-event.h +7 -0
@@ -109,6 +109,8 @@ struct variable_list {/* Command string to events */ extern int parse_perf_probe_command (const char *cmd,struct perf_probe_event *pev); +extern int parse_probe_trace_command (const char *cmd, +struct probe_trace_event *tev);/* Events to command string */ extern char *synthesize_perf_probe_command (struct perf_probe_event *pev);
@@ -121,6 +123,7 @@ extern bool perf_probe_event_need_dwarf(struct perf_probe_event *pev);/* Release event contents */ extern void clear_perf_probe_event (struct perf_probe_event *pev); +extern void clear_probe_trace_event (struct probe_trace_event *tev);/* Command string to line-range */ extern int parse_line_range_desc (const char *cmd,struct line_range *lr);
@@ -144,6 +147,10 @@ bool arch__prefers_symtab(void);void arch__fix_tev_from_maps (struct perf_probe_event *pev,struct probe_trace_event *tev,struct map *map); +/* If there is no space to write, returns -E2BIG. */ +int e_snprintf (char *str,size_t size,const char *format, ...) + __attribute__((format (printf,3 ,4 ))); +/* Maximum index number of event-name postfix */ #define MAX_EVENT_INDEX 1024
tools/perf/util/probe-file.c new file +302 -0
302 lines not on this page · raw message
tools/perf/util/probe-file.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef __PROBE_FILE_H +#define __PROBE_FILE_H + +#include "strlist.h" +#include "strfilter.h" +#include "probe-event.h" + +#define PF_FL_UPROBE 1 +#define PF_FL_RW 2 + +int probe_file__open (int flag); +int probe_file__open_both (int *kfd,int *ufd,int flag); +struct strlist *probe_file__get_namelist (int fd); +struct strlist *probe_file__get_rawlist (int fd); +int probe_file__add_event (int fd,struct probe_trace_event *tev); +int probe_file__del_events (int fd,struct strfilter *filter); + +#endif
RFC PATCH 4/13 perf buildid: Use SBUILD_ID_SIZE macro
In reply to Masami HiramatsuIntroduce SBUILD_ID_SIZE macro and use it instead of using BUILD_ID_SIZE * 2 + 1.
--- tools/perf/builtin-buildid-cache.c | 8 ++++---- tools/perf/builtin-buildid-list.c | 4 ++-- tools/perf/util/build-id.c | 4 ++-- tools/perf/util/build-id.h | 3 ++- 4 files changed, 10 insertions(+), 9 deletions(-)
tools/perf/builtin-buildid-cache.c +4 -4
@@ -127,7 +127,7 @@ static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,static int build_id_cache__add_kcore (const char *filename,bool force) { -char dir[32 ], sbuildid[BUILD_ID_SIZE *2 +1 ]; +char dir[32 ], sbuildid[SBUILD_ID_SIZE ];char from_dir[PATH_MAX ], to_dir[PATH_MAX ];char *p;
@@ -184,7 +184,7 @@ static int build_id_cache__add_kcore(const char *filename, bool force)static int build_id_cache__add_file (const char *filename) { -char sbuild_id[BUILD_ID_SIZE *2 +1 ]; +char sbuild_id[SBUILD_ID_SIZE ];u8 build_id[BUILD_ID_SIZE ];int err;
@@ -204,7 +204,7 @@ static int build_id_cache__add_file(const char *filename)static int build_id_cache__remove_file (const char *filename) {u8 build_id[BUILD_ID_SIZE ]; -char sbuild_id[BUILD_ID_SIZE *2 +1 ]; +char sbuild_id[SBUILD_ID_SIZE ];int err;
@@ -276,7 +276,7 @@ static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fstatic int build_id_cache__update_file (const char *filename) {u8 build_id[BUILD_ID_SIZE ]; -char sbuild_id[BUILD_ID_SIZE *2 +1 ]; +char sbuild_id[SBUILD_ID_SIZE ];int err =0 ;
tools/perf/builtin-buildid-list.c +2 -2
@@ -20,7 +20,7 @@static int sysfs__fprintf_build_id (FILE *fp) {u8 kallsyms_build_id[BUILD_ID_SIZE ]; -char sbuild_id[BUILD_ID_SIZE *2 +1 ]; +char sbuild_id[SBUILD_ID_SIZE ];if (sysfs__read_build_id ("/sys/kernel/notes" , kallsyms_build_id,sizeof (kallsyms_build_id)) !=0 )
@@ -35,7 +35,7 @@ static int sysfs__fprintf_build_id(FILE *fp)static int filename__fprintf_build_id (const char *name,FILE *fp) {u8 build_id[BUILD_ID_SIZE ]; -char sbuild_id[BUILD_ID_SIZE *2 +1 ]; +char sbuild_id[SBUILD_ID_SIZE ];if (filename__read_build_id (name, build_id,sizeof (build_id)) !=sizeof (build_id))
tools/perf/util/build-id.c +2 -2
@@ -132,7 +132,7 @@ char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) {bool is_kallsyms =dso__is_kallsyms ((struct dso *)dso);bool is_vdso =dso__is_vdso ((struct dso *)dso); -char build_id_hex[BUILD_ID_SIZE *2 +1 ]; +char build_id_hex[SBUILD_ID_SIZE ];char *linkname;bool alloc = (bf ==NULL );int ret;
@@ -411,7 +411,7 @@ static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, const char *name, bool is_kallsyms, bool is_vdso) { - char sbuild_id[BUILD_ID_SIZE * 2 + 1]; + char sbuild_id[SBUILD_ID_SIZE];build_id__sprintf (build_id, build_id_size, sbuild_id);
tools/perf/util/build-id.h +2 -1
@@ -1,7 +1,8 @@#ifndef PERF_BUILD_ID_H_ #define PERF_BUILD_ID_H_ 1 -#define BUILD_ID_SIZE 20 +#define BUILD_ID_SIZE 20 +#define SBUILD_ID_SIZE (BUILD_ID_SIZE * 2 + 1)#include "tool.h" #include "strlist.h"
RFC PATCH 5/13 perf buildid: Introduce sysfs/filename__sprintf_build_id
In reply to Masami HiramatsuIntroduce sysfs/filename__sprintf_build_id for consolidating similar code.
--- tools/perf/builtin-buildid-cache.c | 14 ++------------ tools/perf/builtin-buildid-list.c | 24 ++++++++++-------------- tools/perf/util/build-id.c | 32 ++++++++++++++++++++++++++++++++ tools/perf/util/build-id.h | 3 +++ 4 files changed, 47 insertions(+), 26 deletions(-)
tools/perf/builtin-buildid-cache.c +2 -12
@@ -25,8 +25,6 @@static int build_id_cache__kcore_buildid (const char *proc_dir,char *sbuildid) {char root_dir[PATH_MAX ]; -char notes[PATH_MAX ]; -u8 build_id[BUILD_ID_SIZE ];char *p;strlcpy (root_dir, proc_dir,sizeof (root_dir));
@@ -35,15 +33,7 @@ static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)if (!p)return -1 ; *p ='\0' ; - -scnprintf (notes,sizeof (notes),"%s/sys/kernel/notes" , root_dir); - -if (sysfs__read_build_id (notes, build_id,sizeof (build_id))) -return -1 ; - -build_id__sprintf (build_id,sizeof (build_id), sbuildid); - -return 0 ; +return sysfs__sprintf_build_id (root_dir, sbuildid); }static int build_id_cache__kcore_dir (char *dir,size_t sz)
@@ -138,7 +128,7 @@ static int build_id_cache__add_kcore(const char *filename, bool force)return -1 ; *p ='\0' ; -if (build_id_cache__kcore_buildid (from_dir, sbuildid)) +if (build_id_cache__kcore_buildid (from_dir, sbuildid) <0 )return -1 ; scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s",
tools/perf/builtin-buildid-list.c +10 -14
@@ -19,29 +19,25 @@static int sysfs__fprintf_build_id (FILE *fp) { -u8 kallsyms_build_id[BUILD_ID_SIZE ];char sbuild_id[SBUILD_ID_SIZE ]; +int ret; -if (sysfs__read_build_id ("/sys/kernel/notes" , kallsyms_build_id, -sizeof (kallsyms_build_id)) !=0 ) -return -1 ; + ret =sysfs__sprintf_build_id ("/" , sbuild_id); +if (ret !=sizeof (sbuild_id)) +return ret <0 ? ret : -EINVAL ; -build_id__sprintf (kallsyms_build_id,sizeof (kallsyms_build_id), - sbuild_id); -fprintf (fp,"%s\n" , sbuild_id); -return 0 ; +return fprintf (fp,"%s\n" , sbuild_id); }static int filename__fprintf_build_id (const char *name,FILE *fp) { -u8 build_id[BUILD_ID_SIZE ];char sbuild_id[SBUILD_ID_SIZE ]; +int ret; -if (filename__read_build_id (name, build_id, -sizeof (build_id)) !=sizeof (build_id)) -return 0 ; + ret =filename__sprintf_build_id (name, sbuild_id); +if (ret !=sizeof (sbuild_id)) +return ret <0 ? ret : -EINVAL ; -build_id__sprintf (build_id,sizeof (build_id), sbuild_id);return fprintf (fp,"%s\n" , sbuild_id); }
@@ -63,7 +59,7 @@ static int perf_session__list_build_ids(bool force, bool with_hits)/* * See if this is an ELF file first: */ -if (filename__fprintf_build_id (input_name, stdout)) +if (filename__fprintf_build_id (input_name, stdout) >0 ) goto out; session =perf_session__new (&file, false, &build_id__mark_dso_hit_ops);
tools/perf/util/build-id.c +32 -0
@@ -93,6 +93,38 @@ int build_id__sprintf(const u8 *build_id, int len, char *bf)return raw - build_id; } +int sysfs__sprintf_build_id (const char *root_dir,char *sbuild_id) +{ +char notes[PATH_MAX ]; +u8 build_id[BUILD_ID_SIZE ]; +int ret; + +if (!root_dir) + root_dir ="" ; + +scnprintf (notes,sizeof (notes),"%s/sys/kernel/notes" , root_dir); + + ret =sysfs__read_build_id (notes, build_id,sizeof (build_id)); +if (ret <0 ) +return ret; + +return build_id__sprintf (build_id,sizeof (build_id), sbuild_id); +} + +int filename__sprintf_build_id (const char *pathname,char *sbuild_id) +{ +u8 build_id[BUILD_ID_SIZE ]; +int ret; + + ret =filename__read_build_id (pathname, build_id,sizeof (build_id)); +if (ret <0 ) +return ret; +else if (ret !=sizeof (build_id)) +return -EINVAL ; + +return build_id__sprintf (build_id,sizeof (build_id), sbuild_id); +} +/* asnprintf consolidates asprintf and snprintf */ static int asnprintf (char **strp,size_t size,const char *fmt, ...) {
tools/perf/util/build-id.h +3 -0
@@ -12,6 +12,9 @@ extern struct perf_tool build_id__mark_dso_hit_ops;struct dso ;int build_id__sprintf (const u8 *build_id,int len,char *bf); +int sysfs__sprintf_build_id (const char *root_dir,char *sbuild_id); +int filename__sprintf_build_id (const char *pathname,char *sbuild_id); +char *dso__build_id_filename (const struct dso *dso,char *bf,size_t size); int build_id__mark_dso_hit(struct perf_tool *tool, union perf_event *event,
RFC PATCH 6/13 perf: Add lsdir to read a directory
In reply to Masami HiramatsuAs a utility function, add lsdir() which reads given directory and store entry name into a strlist. lsdir accepts a filter function so that user can filter out unneeded entries.
--- tools/perf/util/util.c | 34 ++++++++++++++++++++++++++++++++++ tools/perf/util/util.h | 4 ++++ 2 files changed, 38 insertions(+)
tools/perf/util/util.c +34 -0
@@ -115,6 +115,40 @@ int rm_rf(char *path)return rmdir (path); } +/* A filter which removes dot files */ +bool lsdir_no_dot_filter (const char *dirname__maybe_unused ,struct dirent *d) +{ +return d->d_name[0 ] !='.' ; +} + +/* lsdir reads a directory and store it in strlist */ +struct strlist *lsdir (const char *dirname, +bool (*filter)(const char *,struct dirent *)) +{ +struct strlist *list =NULL ; +DIR *dir; +struct dirent *d; + + dir =opendir (dirname); +if (!dir) +return NULL ; + + list =strlist__new (true,NULL ); +if (!list) { + errno = -ENOMEM ; + goto out; + } + +while ((d =readdir (dir)) !=NULL ) { +if (!filter ||filter (dirname, d)) +strlist__add (list, d->d_name); + } + +out: +closedir (dir); +return list; +} +static int slow_copyfile (const char *from,const char *to,mode_t mode) {int err =-1 ;
tools/perf/util/util.h +4 -0
@@ -79,6 +79,7 @@#include <termios.h> #include <linux/bitops.h> #include <termios.h> +#include "strlist.h" extern const char *graph_line;extern const char *graph_dotted_line;
@@ -250,6 +251,9 @@ static inline int sane_case(int x, int high)int mkdir_p (char *path,mode_t mode);int rm_rf (char *path); +struct strlist *lsdir (const char *dirname, +bool (*filter)(const char *,struct dirent *)); +bool lsdir_no_dot_filter (const char *dirname__maybe_unused ,struct dirent *d);int copyfile (const char *from,const char *to);int copyfile_mode (const char *from,const char *to,mode_t mode);int copyfile_offset (int fromfd,loff_t from_ofs,int tofd,loff_t to_ofs,u64 size);
RFC PATCH 7/13 perf-buildid-cache: Use lsdir for looking up buildid caches
In reply to Masami HiramatsuUse new lsdir() for looking up buildid caches. This changes logic a bit to ignore all dot files, since the build-id cache must not start with dot.
--- tools/perf/util/build-id.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-)
tools/perf/util/build-id.c +5 -25
@@ -342,38 +342,18 @@ int build_id_cache__list_build_ids(const char *pathname, {struct strlist *list;char *dir_name; -DIR *dir; -struct dirent *d;int ret =0 ; - list =strlist__new (true,NULL ); dir_name =build_id_cache__dirname_from_path (pathname, false, false,NULL ); -if (!list || !dir_name) { - ret = -ENOMEM ; - goto out; - } +if (!dir_name) +return -ENOMEM ; -/* List up all dirents */ - dir =opendir (dir_name); -if (!dir) { + list =lsdir (dir_name, lsdir_no_dot_filter); +if (!list) ret = -errno; - goto out; - } - -while ((d =readdir (dir)) !=NULL ) { -if (!strcmp (d->d_name,"." ) || !strcmp (d->d_name,".." )) -continue ; -strlist__add (list, d->d_name); - } -closedir (dir); - -out: + *result = list;free (dir_name); -if (ret) -strlist__delete (list); -else - *result = list;return ret; }
RFC PATCH 8/13 perf probe: Check kprobe blacklist in converting phase
In reply to Masami HiramatsuInstead of checking kprobe blacklist in writing phase, check it in converting phase.
--- tools/perf/util/probe-event.c | 92 ++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 38 deletions(-)
tools/perf/util/probe-event.c +54 -38
@@ -549,6 +549,8 @@ static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,return ret; } +static bool kprobe_blacklist__listed (unsigned long address); +/* Post processing the probe events */ static int post_process_probe_trace_events(struct probe_trace_event *tevs, int ntevs, const char *module,
@@ -575,27 +577,32 @@ static int post_process_probe_trace_events(struct probe_trace_event *tevs, etext_addr =kernel_get_symbol_address_by_name ("_etext" , false);for (i =0 ; i < ntevs; i++) { -if (tevs[i].point.address && !tevs[i].point.retprobe) { -/* If we found a wrong one, mark it by NULL symbol */ -if (etext_addr < tevs[i].point.address) { -pr_warning ("%s+%lu is out of .text, skip it.\n" , - tevs[i].point.symbol, tevs[i].point.offset); - tmp =NULL ; - skipped++; - }else { - tmp =strdup (reloc_sym->name); -if (!tmp) -return -ENOMEM ; - } -/* If we have no realname, use symbol for it */ -if (!tevs[i].point.realname) - tevs[i].point.realname = tevs[i].point.symbol; -else -free (tevs[i].point.symbol); - tevs[i].point.symbol = tmp; - tevs[i].point.offset = tevs[i].point.address - - reloc_sym->unrelocated_addr; +if (!tevs[i].point.address || tevs[i].point.retprobe) +continue ; +/* If we found a wrong one, mark it by NULL symbol */ +if (etext_addr < tevs[i].point.address) { +pr_warning ("%s+%lu is out of .text, skip it.\n" , + tevs[i].point.symbol, tevs[i].point.offset); + tmp =NULL ; + skipped++; + }else if (kprobe_blacklist__listed (tevs[i].point.address)) { +pr_warning ("%s is blacklisted function, skip it.\n" , + tevs[i].point.symbol); + tmp =NULL ; + skipped++; + }else { + tmp =strdup (reloc_sym->name); +if (!tmp) +return -ENOMEM ; } +/* If we have no realname, use symbol for it */ +if (!tevs[i].point.realname) + tevs[i].point.realname = tevs[i].point.symbol; +else +free (tevs[i].point.symbol); + tevs[i].point.symbol = tmp; + tevs[i].point.offset = tevs[i].point.address - + reloc_sym->unrelocated_addr; }return skipped; }
@@ -2008,6 +2015,27 @@ kprobe_blacklist__find_by_address(struct list_head *blacklist,return NULL ; } +static LIST_HEAD (kprobe_blacklist); + +static void kprobe_blacklist__init (void ) +{ +if (!list_empty (&kprobe_blacklist)) +return ; + +if (kprobe_blacklist__load (&kprobe_blacklist) <0 ) +pr_debug ("No kprobe blacklist support, ignored\n" ); +} + +static void kprobe_blacklist__release (void ) +{ +kprobe_blacklist__delete (&kprobe_blacklist); +} + +static bool kprobe_blacklist__listed (unsigned long address) +{ +return !!kprobe_blacklist__find_by_address (&kprobe_blacklist, address); +} +/* Show an event */ static int show_perf_probe_event(const char *group, const char *event, struct perf_probe_event *pev,
@@ -2236,8 +2264,6 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,int i, fd, ret;struct probe_trace_event *tev =NULL ;struct strlist *namelist; -LIST_HEAD (blacklist); -struct kprobe_blacklist_node *node; fd =probe_file__open (PF_FL_RW | (pev->uprobes ?PF_FL_UPROBE :0 ));if (fd <0 )
@@ -2251,27 +2277,13 @@ static int __add_probe_trace_events(struct perf_probe_event *pev, goto close_out; } -/* Get kprobe blacklist if exists */ -if (!pev->uprobes) { - ret =kprobe_blacklist__load (&blacklist); -if (ret <0 ) -pr_debug ("No kprobe blacklist support, ignored\n" ); - } - ret =0 ;pr_info ("Added new event%s\n" , (ntevs >1 ) ?"s:" :":" );for (i =0 ; i < ntevs; i++) { tev = &tevs[i]; -/* Skip if the symbol is out of .text (marked previously) */ +/* Skip if the symbol is out of .text or blacklisted */ if (!tev->point.symbol)continue ; -/* Ensure that the address is NOT blacklisted */ - node =kprobe_blacklist__find_by_address (&blacklist, - tev->point.address); -if (node) { -pr_warning ("Warning: Skipped probing on blacklisted function: %s\n" , node->symbol); -continue ; - }/* Set new name for tev (and update namelist) */ ret = probe_trace_event__set_name(tev, pev, namelist,
@@ -2305,7 +2317,6 @@ static int __add_probe_trace_events(struct perf_probe_event *pev, tev->event); } -kprobe_blacklist__delete (&blacklist);strlist__delete (namelist); close_out:close (fd);
@@ -2513,6 +2524,9 @@ int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)/* Loop 1: convert all events */ for (i =0 ; i < npevs; i++) { pkgs[i].pev = &pevs[i]; +/* Init kprobe blacklist if needed */ +if (!pkgs[i].pev->uprobes) +kprobe_blacklist__init ();/* Convert with or without debuginfo */ ret =convert_to_probe_trace_events (pkgs[i].pev, &pkgs[i].tevs);
@@ -2520,6 +2534,8 @@ int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) goto end; pkgs[i].ntevs = ret; } +/* This just release blacklist only if allocated */ +kprobe_blacklist__release ();/* Loop 2: add all events */ for (i =0 ; i < npevs; i++) {
RFC PATCH 9/13 perf probe: Use strbuf for making strings in probe-event.c
In reply to Masami HiramatsuReplace many fixed-length char array with strbuf to stringify perf_probe_event and probe_trace_event etc. in util/probe-event.c.
--- tools/perf/util/probe-event.c | 225 ++++++++++++++-------------------------- tools/perf/util/probe-event.h | 3 - tools/perf/util/probe-finder.c | 10 +- 3 files changed, 85 insertions(+), 153 deletions(-)
tools/perf/util/probe-event.c +79 -146
@@ -1517,69 +1517,52 @@ out: }/* Compose only probe arg */ -int synthesize_perf_probe_arg (struct perf_probe_arg *pa,char *buf,size_t len) +char *synthesize_perf_probe_arg (struct perf_probe_arg *pa) {struct perf_probe_arg_field *field = pa->field; -int ret; -char *tmp = buf; +struct strbuf buf; +char *ret; +strbuf_init (&buf,64 );if (pa->name && pa->var) - ret =e_snprintf (tmp, len,"%s=%s" , pa->name, pa->var); +strbuf_addf (&buf,"%s=%s" , pa->name, pa->var);else - ret =e_snprintf (tmp, len,"%s" , pa->name ? pa->name : pa->var); -if (ret <=0 ) - goto error; - tmp += ret; - len -= ret; +strbuf_addstr (&buf, pa->name ?: pa->var);while (field) {if (field->name[0 ] =='[' ) - ret =e_snprintf (tmp, len,"%s" , field->name); +strbuf_addstr (&buf, field->name);else - ret =e_snprintf (tmp, len,"%s%s" , - field->ref ?"->" :"." , field->name); -if (ret <=0 ) - goto error; - tmp += ret; - len -= ret; +strbuf_addf (&buf,"%s%s" , field->ref ?"->" :"." , + field->name); field = field->next; } -if (pa->type) { - ret =e_snprintf (tmp, len,":%s" , pa->type); -if (ret <=0 ) - goto error; - tmp += ret; - len -= ret; - } +if (pa->type) +strbuf_addf (&buf,":%s" , pa->type); + + ret =strbuf_detach (&buf,NULL ); +strbuf_release (&buf); -return tmp - buf; -error: -pr_debug ("Failed to synthesize perf probe argument: %d\n" , ret);return ret; }/* Compose only probe point (not argument) */ static char *synthesize_perf_probe_point (struct perf_probe_point *pp) { -char *buf, *tmp; -char offs[32 ] ="" , line[32 ] ="" , file[32 ] ="" ; -int ret, len; - - buf =zalloc (MAX_CMDLEN ); -if (buf ==NULL ) { - ret = -ENOMEM ; - goto error; - } -if (pp->offset) { - ret =e_snprintf (offs,32 ,"+%lu" , pp->offset); -if (ret <=0 ) - goto error; - } -if (pp->line) { - ret =e_snprintf (line,32 ,":%d" , pp->line); -if (ret <=0 ) - goto error; +struct strbuf buf; +char *tmp; +int len; + +strbuf_init (&buf,64 ); +if (pp->function) { +strbuf_addstr (&buf, pp->function); +if (pp->offset) +strbuf_addf (&buf,"+%lu" , pp->offset); +else if (pp->line) +strbuf_addf (&buf,":%d" , pp->line); +else if (pp->retprobe) +strbuf_addstr (&buf,"%return" ); }if (pp->file) { tmp = pp->file;
@@ -1588,25 +1571,14 @@ static char *synthesize_perf_probe_point(struct perf_probe_point *pp) tmp =strchr (pp->file + len -30 ,'/' ); tmp = tmp ? tmp +1 : pp->file + len -30 ; } - ret =e_snprintf (file,32 ,"@%s" , tmp); -if (ret <=0 ) - goto error; +strbuf_addf (&buf,"@%s" , tmp); +if (!pp->function && pp->line) +strbuf_addf (&buf,":%d" , pp->line); } -if (pp->function) - ret =e_snprintf (buf,MAX_CMDLEN ,"%s%s%s%s%s" , pp->function, - offs, pp->retprobe ?"%return" :"" , line, - file); -else - ret =e_snprintf (buf,MAX_CMDLEN ,"%s%s" , file, line); -if (ret <=0 ) - goto error; - -return buf; -error: -pr_debug ("Failed to synthesize perf probe point: %d\n" , ret); -free (buf); -return NULL ; + tmp =strbuf_detach (&buf,NULL ); +strbuf_release (&buf); +return tmp; }#if 0
@@ -1635,45 +1607,30 @@ char *synthesize_perf_probe_command(struct perf_probe_event *pev)#endif static int __synthesize_probe_trace_arg_ref (struct probe_trace_arg_ref *ref, -char **buf,size_t *buflen, -int depth) +struct strbuf *buf,int depth) { -int ret;if (ref->next) { depth =__synthesize_probe_trace_arg_ref (ref->next, buf, - buflen, depth +1 ); + depth +1 );if (depth <0 ) goto out; } - - ret =e_snprintf (*buf, *buflen,"%+ld(" , ref->offset); -if (ret <0 ) - depth = ret; -else { - *buf += ret; - *buflen -= ret; - } +strbuf_addf (buf,"%+ld(" , ref->offset); out:return depth; - }static int synthesize_probe_trace_arg (struct probe_trace_arg *arg, -char *buf,size_t buflen) +struct strbuf *buf) {struct probe_trace_arg_ref *ref = arg->ref; -int ret, depth =0 ; -char *tmp = buf; +int depth =0 ;/* Argument name or separator */ if (arg->name) - ret =e_snprintf (buf, buflen," %s=" , arg->name); +strbuf_addf (buf," %s=" , arg->name);else - ret =e_snprintf (buf, buflen," " ); -if (ret <0 ) -return ret; - buf += ret; - buflen -= ret; +strbuf_addch (buf,' ' );/* Special case: @XXX */ if (arg->value[0 ] =='@' && arg->ref)
@@ -1681,86 +1638,58 @@ static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,/* Dereferencing arguments */ if (ref) { - depth =__synthesize_probe_trace_arg_ref (ref, &buf, - &buflen,1 ); + depth =__synthesize_probe_trace_arg_ref (ref, buf,1 );if (depth <0 )return depth; }/* Print argument value */ if (arg->value[0 ] =='@' && arg->ref) - ret =e_snprintf (buf, buflen,"%s%+ld" , arg->value, - arg->ref->offset); +strbuf_addf (buf,"%s%+ld" , arg->value, arg->ref->offset);else - ret =e_snprintf (buf, buflen,"%s" , arg->value); -if (ret <0 ) -return ret; - buf += ret; - buflen -= ret; +strbuf_addstr (buf, arg->value);/* Closing */ -while (depth--) { - ret =e_snprintf (buf, buflen,")" ); -if (ret <0 ) -return ret; - buf += ret; - buflen -= ret; - } +while (depth--) +strbuf_addch (buf,')' );/* Print argument type */ -if (arg->type) { - ret =e_snprintf (buf, buflen,":%s" , arg->type); -if (ret <=0 ) -return ret; - buf += ret; - } +if (arg->type) +strbuf_addf (buf,":%s" , arg->type); -return buf - tmp; +return 0 ; }char *synthesize_probe_trace_command (struct probe_trace_event *tev) {struct probe_trace_point *tp = &tev->point; -char *buf; -int i, len, ret; - - buf =zalloc (MAX_CMDLEN ); -if (buf ==NULL ) -return NULL ; - - len =e_snprintf (buf,MAX_CMDLEN ,"%c:%s/%s " , tp->retprobe ?'r' :'p' , - tev->group, tev->event); -if (len <=0 ) - goto error; +struct strbuf buf; +char *ret =NULL ; +int i;/* Uprobes must have tp->address and tp->module */ if (tev->uprobes && (!tp->address || !tp->module)) - goto error; +return NULL ; + +strbuf_init (&buf,32 ); +strbuf_addf (&buf,"%c:%s/%s " , tp->retprobe ?'r' :'p' , + tev->group, tev->event);/* Use the tp->address for uprobes */ if (tev->uprobes) - ret =e_snprintf (buf + len,MAX_CMDLEN - len,"%s:0x%lx" , - tp->module, tp->address); +strbuf_addf (&buf,"%s:0x%lx" , tp->module, tp->address);else - ret =e_snprintf (buf + len,MAX_CMDLEN - len,"%s%s%s+%lu" , - tp->module ?:"" , tp->module ?":" :"" , - tp->symbol, tp->offset); - -if (ret <=0 ) - goto error; - len += ret; +strbuf_addf (&buf,"%s%s%s+%lu" , tp->module ?:"" , + tp->module ?":" :"" , tp->symbol, tp->offset);for (i =0 ; i < tev->nargs; i++) { - ret =synthesize_probe_trace_arg (&tev->args[i], buf + len, -MAX_CMDLEN - len); -if (ret <=0 ) +if (synthesize_probe_trace_arg (&tev->args[i], &buf) <0 ) goto error; - len += ret; } -return buf; + ret =strbuf_detach (&buf,NULL ); error: -free (buf); -return NULL ; +strbuf_release (&buf); +return ret; } static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
@@ -1838,7 +1767,7 @@ static int convert_to_perf_probe_point(struct probe_trace_point *tp,static int convert_to_perf_probe_event (struct probe_trace_event *tev,struct perf_probe_event *pev,bool is_kprobe) { -char buf[64 ] ="" ; +struct strbuf buf =STRBUF_INIT ;int i, ret;/* Convert event/group name */
@@ -1861,9 +1790,10 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,if (tev->args[i].name) pev->args[i].name =strdup (tev->args[i].name);else { - ret =synthesize_probe_trace_arg (&tev->args[i], - buf,64 ); - pev->args[i].name =strdup (buf); +strbuf_init (&buf,32 ); + ret =synthesize_probe_trace_arg (&tev->args[i], &buf); + pev->args[i].name =strbuf_detach (&buf,NULL ); +strbuf_release (&buf); }if (pev->args[i].name ==NULL && ret >=0 ) ret = -ENOMEM ;
@@ -2042,33 +1972,36 @@ static int show_perf_probe_event(const char *group, const char *event, const char *module) {int i, ret; -char buf[128 ]; -char *place; +char *buf, *place;/* Synthesize only event probe point */ place =synthesize_perf_probe_point (&pev->point);if (!place)return -EINVAL ; - ret =e_snprintf (buf,128 ,"%s:%s" , group, event); + ret =asprintf (&buf,"%s:%s" , group, event);if (ret <0 ) -return ret; +return -errno;pr_info (" %-20s (on %s" , buf, place); +free (buf);if (module)pr_info (" in %s" , module);if (pev->nargs >0 ) {pr_info (" with" );for (i =0 ; i < pev->nargs; i++) { - ret =synthesize_perf_probe_arg (&pev->args[i], - buf,128 ); -if (ret <0 ) -break ; + buf =synthesize_perf_probe_arg (&pev->args[i]); +if (!buf) { + ret = -ENOMEM ; + goto out; + }pr_info (" %s" , buf); +free (buf); } }pr_info (")\n" ); +out:free (place);return ret; }
tools/perf/util/probe-event.h +1 -2
@@ -115,8 +115,7 @@ extern int parse_probe_trace_command(const char *cmd,/* Events to command string */ extern char *synthesize_perf_probe_command (struct perf_probe_event *pev);extern char *synthesize_probe_trace_command (struct probe_trace_event *tev); -extern int synthesize_perf_probe_arg (struct perf_probe_arg *pa,char *buf, -size_t len); +extern char *synthesize_perf_probe_arg (struct perf_probe_arg *pa);/* Check the perf_probe_event needs debuginfo */ extern bool perf_probe_event_need_dwarf (struct perf_probe_event *pev);
tools/perf/util/probe-finder.c +5 -5
@@ -550,7 +550,7 @@ static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)static int find_variable (Dwarf_Die *sc_die,struct probe_finder *pf) {Dwarf_Die vr_die; -char buf[32 ], *ptr; +char *buf, *ptr;int ret =0 ;if (!is_c_varname (pf->pvar->var)) {
@@ -575,13 +575,13 @@ static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)if (pf->pvar->name) pf->tvar->name =strdup (pf->pvar->name);else { - ret =synthesize_perf_probe_arg (pf->pvar, buf,32 ); -if (ret <0 ) -return ret; + buf =synthesize_perf_probe_arg (pf->pvar); +if (!buf) +return -ENOMEM ; ptr =strchr (buf,':' );/* Change type separator to _ */ if (ptr) *ptr ='_' ; - pf->tvar->name =strdup (buf); + pf->tvar->name = buf; }if (pf->tvar->name ==NULL )return -ENOMEM ;
RFC PATCH 10/13 perf probe: Add --cache option to cache the probe definitions
In reply to Masami HiramatsuAdd --cache option to cache the probe definitions. This just saves the result of the dwarf analysis to probe cache.
--- tools/perf/builtin-probe.c | 1 tools/perf/util/build-id.c | 13 +- tools/perf/util/build-id.h | 2 tools/perf/util/probe-event.c | 136 +++++++++++++++---- tools/perf/util/probe-event.h | 5 + tools/perf/util/probe-file.c | 298 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/probe-file.h | 20 +++ 7 files changed, 443 insertions(+), 32 deletions(-)
tools/perf/builtin-probe.c +1 -0
@@ -397,6 +397,7 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) opt_set_filter),OPT_CALLBACK ('x' ,"exec" ,NULL ,"executable|path" ,"target executable name or path" , opt_set_target), +OPT_BOOLEAN (0 ,"cache" , &probe_conf.cache,"Manipulate probe cache" ),OPT_BOOLEAN (0 ,"demangle" , &symbol_conf.demangle,"Enable symbol demangling" ), OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
tools/perf/util/build-id.c +6 -7
@@ -313,9 +313,8 @@ void disable_buildid_cache(void) no_buildid_cache = true; } -static char *build_id_cache__dirname_from_path (const char *name, -bool is_kallsyms,bool is_vdso, -const char *sbuild_id) +char *build_id_cache__dirname_from_path (const char *sbuild_id,const char *name, +bool is_kallsyms,bool is_vdso) {char *realname = (char *)name, *filename;bool slash = is_kallsyms || is_vdso;
@@ -344,8 +343,8 @@ int build_id_cache__list_build_ids(const char *pathname,char *dir_name;int ret =0 ; - dir_name =build_id_cache__dirname_from_path (pathname, false, false, -NULL ); + dir_name =build_id_cache__dirname_from_path (NULL , pathname, + false, false);if (!dir_name)return -ENOMEM ;
@@ -372,8 +371,8 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name, goto out_free; } - dir_name =build_id_cache__dirname_from_path (name, is_kallsyms, - is_vdso, sbuild_id); + dir_name =build_id_cache__dirname_from_path (sbuild_id, name, + is_kallsyms, is_vdso);if (!dir_name) goto out_free;
tools/perf/util/build-id.h +2 -0
@@ -27,6 +27,8 @@ bool perf_session__read_build_ids(struct perf_session *session, bool with_hits);int perf_session__write_buildid_table (struct perf_session *session,int fd);int perf_session__cache_build_ids (struct perf_session *session); +char *build_id_cache__dirname_from_path (const char *sbuild_id,const char *name, +bool is_kallsyms,bool is_vdso);int build_id_cache__list_build_ids (const char *pathname,struct strlist **result);bool build_id_cache__cached (const char *sbuild_id);
tools/perf/util/probe-event.c +111 -25
197 lines not on this page · raw message
tools/perf/util/probe-event.h +5 -0
@@ -12,6 +12,7 @@ struct probe_conf {bool show_location_range;bool force_add;bool no_inlines; +bool cache;int max_probes; };extern struct probe_conf probe_conf;
@@ -116,6 +117,10 @@ extern int parse_probe_trace_command(const char *cmd,extern char *synthesize_perf_probe_command (struct perf_probe_event *pev);extern char *synthesize_probe_trace_command (struct probe_trace_event *tev);extern char *synthesize_perf_probe_arg (struct perf_probe_arg *pa); +char *synthesize_perf_probe_point (struct perf_probe_point *pp); + +int perf_probe_event__copy (struct perf_probe_event *dst, +struct perf_probe_event *src);/* Check the perf_probe_event needs debuginfo */ extern bool perf_probe_event_need_dwarf (struct perf_probe_event *pev);
tools/perf/util/probe-file.c +298 -0
307 lines not on this page · raw message
tools/perf/util/probe-file.h +20 -0
@@ -15,4 +15,24 @@ struct strlist *probe_file__get_rawlist(int fd);int probe_file__add_event (int fd,struct probe_trace_event *tev);int probe_file__del_events (int fd,struct strfilter *filter); +/* Cache of probe definitions */ +struct probe_cache_entry { +struct list_head list; +struct perf_probe_event pev; +char *spev; +struct strlist *tevlist; +}; + +struct probe_cache { +int fd; +struct list_head list; +}; + +struct probe_cache *probe_cache__new (const char *target); +int probe_cache__add_entry (struct probe_cache *pcache, +struct perf_probe_event *pev, +struct probe_trace_event *tevs,int ntevs); +int probe_cache__commit (struct probe_cache *pcache); +void probe_cache__delete (struct probe_cache *pcache); +#endif
RFC PATCH 11/13 perf probe: Use cache entry if possible
In reply to Masami HiramatsuBefore analyzing debuginfo, try to find a corresponding entry from probe cache always. This does not depend on --cache, the --cache enables to store/update cache, but looking up the cache is always enabled.
--- tools/perf/util/probe-event.c | 58 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/probe-file.c | 20 +++++++++++++- tools/perf/util/probe-file.h | 5 +++- 3 files changed, 81 insertions(+), 2 deletions(-)
tools/perf/util/probe-event.c +58 -0
@@ -2489,6 +2489,59 @@ err_out:bool __weak arch__prefers_symtab (void ) {return false; } +static int find_probe_trace_events_from_cache (struct perf_probe_event *pev, +struct probe_trace_event **tevs) +{ +struct probe_cache *cache; +struct probe_cache_entry *entry; +struct probe_trace_event *tev; +struct str_node *node; +int ret, i; + + cache =probe_cache__new (pev->target); +if (!cache) +return 0 ; + + entry =probe_cache__find (cache, pev); + +if (!entry && !pev->event && + (!pev->point.file && pev->point.function && !pev->point.retprobe && + !pev->point.line && !pev->point.offset && !pev->point.lazy_line)) { + entry =probe_cache__find_by_name (cache, pev->group, + pev->point.function); + } +if (!entry) { + ret =0 ; + goto out; + } + + ret =strlist__nr_entries (entry->tevlist); +if (ret > probe_conf.max_probes) { +pr_debug ("Too many entries matched in the cache of %s\n" , + pev->target ? :"kernel" ); + ret = -E2BIG ; + goto out; + } + + *tevs =zalloc (ret *sizeof (*tev)); +if (!*tevs) { + ret = -ENOMEM ; + goto out; + } + + i =0 ; +strlist__for_each (node, entry->tevlist) { + ret =parse_probe_trace_command (node->s, &(*tevs)[i++]); +if (ret <0 ) + goto out; + } + ret = i; + +out: +probe_cache__delete (cache); +return ret; +} +static int convert_to_probe_trace_events (struct perf_probe_event *pev,struct probe_trace_event **tevs) {
@@ -2503,6 +2556,11 @@ static int convert_to_probe_trace_events(struct perf_probe_event *pev, } } +/* At first, we need to lookup cache entry */ + ret =find_probe_trace_events_from_cache (pev, tevs); +if (ret >0 ) +return ret;/* Found in probe cache */ +if (arch__prefers_symtab () && !perf_probe_event_need_dwarf (pev)) { ret =find_probe_trace_events_from_map (pev, tevs);if (ret >0 )
tools/perf/util/probe-file.c +19 -1
@@ -481,7 +481,7 @@ static bool streql(const char *a, const char *b)return !strcmp (a, b); } -static struct probe_cache_entry * +struct probe_cache_entry *probe_cache__find (struct probe_cache *pcache,struct perf_probe_event *pev) {struct probe_cache_entry *entry =NULL ;
@@ -506,6 +506,24 @@ found:return entry; } +struct probe_cache_entry * +probe_cache__find_by_name (struct probe_cache *pcache, +const char *group,const char *event) +{ +struct probe_cache_entry *entry =NULL ; + +list_for_each_entry (entry, &pcache->list, list) { +/* Hit if same event name or same command-string */ +if (streql (entry->pev.group, group) && +streql (entry->pev.event, event)) + goto found; + } + entry =NULL ; + +found: +return entry; +} +int probe_cache__add_entry (struct probe_cache *pcache,struct perf_probe_event *pev,struct probe_trace_event *tevs,int ntevs)
tools/perf/util/probe-file.h +4 -1
@@ -34,5 +34,8 @@ int probe_cache__add_entry(struct probe_cache *pcache, struct probe_trace_event *tevs, int ntevs);int probe_cache__commit (struct probe_cache *pcache);void probe_cache__delete (struct probe_cache *pcache); - +struct probe_cache_entry *probe_cache__find (struct probe_cache *pcache, +struct perf_probe_event *pev); +struct probe_cache_entry *probe_cache__find_by_name (struct probe_cache *pcache, +const char *group,const char *event);#endif
RFC PATCH 12/13 perf probe: Show all cached probes
In reply to Masami Hiramatsuperf probe --list shows all cached probes when --cache is given. Each caches are shown with on which binary that probed. e.g.
-----
# perf probe --cache vfs_read \$params # perf probe --cache -x /lib64/libc-2.17.so getaddrinfo \$params # ./perf probe --cache --list /[kernel.kallsyms] (1466a0a250b5d0070c6d0f03c5fed30b237970a1): vfs_read $params /usr/lib64/libc-2.17.so (c31ffe7942bfd77b2fca8f9bd5709d387a86d3bc): getaddrinfo $params ----- Note that $params requires debuginfo.
--- tools/perf/builtin-probe.c | 2 + tools/perf/util/build-id.c | 79 ++++++++++++++++++++++++++++++++++++++++- tools/perf/util/build-id.h | 3 ++ tools/perf/util/probe-event.c | 3 ++ tools/perf/util/probe-file.c | 63 ++++++++++++++++++++++++++++++++- tools/perf/util/probe-file.h | 1 + 6 files changed, 146 insertions(+), 5 deletions(-)
tools/perf/builtin-probe.c +1 -1
@@ -44,7 +44,7 @@#define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"#define DEFAULT_FUNC_FILTER "!_*" -#define DEFAULT_LIST_FILTER "*:*" +#define DEFAULT_LIST_FILTER "*"/* Session management structure */ static struct {
tools/perf/util/build-id.c +77 -2
97 lines not on this page · raw message
tools/perf/util/build-id.h +3 -0
@@ -27,8 +27,11 @@ bool perf_session__read_build_ids(struct perf_session *session, bool with_hits);int perf_session__write_buildid_table (struct perf_session *session,int fd);int perf_session__cache_build_ids (struct perf_session *session); +char *build_id_cache__origname (const char *sbuild_id); +char *build_id_cache__linkname (const char *sbuild_id,char *bf,size_t size);char *build_id_cache__dirname_from_path (const char *sbuild_id,const char *name,bool is_kallsyms,bool is_vdso); +int build_id_cache__list_all (struct strlist **result);int build_id_cache__list_build_ids (const char *pathname,struct strlist **result);bool build_id_cache__cached (const char *sbuild_id);
tools/perf/util/probe-event.c +3 -0
@@ -2144,6 +2144,9 @@ int show_perf_probe_events(struct strfilter *filter)setup_pager (); +if (probe_conf.cache) +return probe_cache__show_all_caches (filter); + ret =init_symbol_maps (false);if (ret <0 )return ret;
tools/perf/util/probe-file.c +61 -2
82 lines not on this page · raw message
tools/perf/util/probe-file.h +1 -0
@@ -38,4 +38,5 @@ struct probe_cache_entry *probe_cache__find(struct probe_cache *pcache, struct perf_probe_event *pev);struct probe_cache_entry *probe_cache__find_by_name (struct probe_cache *pcache,const char *group,const char *event); +int probe_cache__show_all_caches (struct strfilter *filter);#endif
RFC PATCH 13/13 perf probe: Remove caches when --cache is given
In reply to Masami Hiramatsuperf-probe --del removes caches when --cache is given. Note that the delete pattern is not same as normal events.
If you cached probes with event name, --del "eventname" works as expected. However, if you skipped it, the cached probes doesn't have actual event name. In that case
--del "probe-desc" is required. For example a cache entry
has the probe-desc "vfs_read $params", you can remove it with --del "vfs_read*\$params".
-----
# perf probe --cache --list /[kernel.kallsyms] (1466a0a250b5d0070c6d0f03c5fed30b237970a1): vfs_read $params /usr/lib64/libc-2.17.so (c31ffe7942bfd77b2fca8f9bd5709d387a86d3bc): getaddrinfo $params
# perf probe --cache --del vfs_read\* Removed event: probe:vfs_read
# perf probe --cache --list /[kernel.kallsyms] (1466a0a250b5d0070c6d0f03c5fed30b237970a1): /usr/lib64/libc-2.17.so (c31ffe7942bfd77b2fca8f9bd5709d387a86d3bc): getaddrinfo $params -----
--- tools/perf/util/probe-event.c | 26 ++++++++++++++++++++++++++ tools/perf/util/probe-file.c | 33 +++++++++++++++++++++++++-------- tools/perf/util/probe-file.h | 2 ++ 3 files changed, 53 insertions(+), 8 deletions(-)
tools/perf/util/probe-event.c +26 -0
@@ -2638,6 +2638,30 @@ end:return ret; } +static int del_perf_probe_caches (struct strfilter *filter) +{ +struct probe_cache *cache; +struct strlist *bidlist; +struct str_node *nd; +int ret; + + ret =build_id_cache__list_all (&bidlist); +if (ret <0 ) { +pr_debug ("Failed to get buildids: %d\n" , ret); +return ret; + } + +strlist__for_each (nd, bidlist) { + cache =probe_cache__new (nd->s); +if (!cache) +continue ; +probe_cache__remove_entries (cache, filter); +probe_cache__commit (cache); +probe_cache__delete (cache); + } +return 0 ; +} +int del_perf_probe_events (struct strfilter *filter) {int ret, ret2, ufd =-1 , kfd =-1 ;
@@ -2647,6 +2671,8 @@ int del_perf_probe_events(struct strfilter *filter)return -EINVAL ;pr_debug ("Delete filter: \'%s\'\n" , str); +if (probe_conf.cache) +return del_perf_probe_caches (filter);/* Get current event names */ ret =probe_file__open_both (&kfd, &ufd,PF_FL_RW );
tools/perf/util/probe-file.c +25 -8
@@ -371,7 +371,6 @@ static int probe_cache__open(struct probe_cache *pcache, const char *target)return ret; } } - dir_name =build_id_cache__dirname_from_path (sbuildid, target, is_kallsyms, false); found:
@@ -627,19 +626,37 @@ out:return ret; } +static bool probe_cache_entry__compare (struct probe_cache_entry *entry, +struct strfilter *filter) +{ +char buf[128 ], *ptr = entry->spev; + +if (entry->pev.event) { +snprintf (buf,128 ,"%s:%s" , entry->pev.group, entry->pev.event); + ptr = buf; + } +return strfilter__compare (filter, ptr); +} + +int probe_cache__remove_entries (struct probe_cache *pcache, +struct strfilter *filter) +{ +struct probe_cache_entry *entry, *tmp; + +list_for_each_entry_safe (entry, tmp, &pcache->list, list) { +if (probe_cache_entry__compare (entry, filter)) +probe_cache_entry__delete (entry); + } +return 0 ; +} +static int probe_cache__show_entries (struct probe_cache *pcache,struct strfilter *filter) {struct probe_cache_entry *entry; -char buf[128 ], *ptr;list_for_each_entry (entry, &pcache->list, list) { -if (entry->pev.event) { - ptr = buf; -snprintf (buf,128 ,"%s:%s" , entry->pev.group, entry->pev.event); - }else - ptr = entry->spev; -if (strfilter__compare (filter, ptr)) +if (probe_cache_entry__compare (entry, filter))printf ("%s\n" , entry->spev); }return 0 ;
tools/perf/util/probe-file.h +2 -0
@@ -34,6 +34,8 @@ int probe_cache__add_entry(struct probe_cache *pcache, struct probe_trace_event *tevs, int ntevs);int probe_cache__commit (struct probe_cache *pcache);void probe_cache__delete (struct probe_cache *pcache); +int probe_cache__remove_entries (struct probe_cache *pcache, +struct strfilter *filter);struct probe_cache_entry *probe_cache__find (struct probe_cache *pcache,struct perf_probe_event *pev); struct probe_cache_entry *probe_cache__find_by_name(struct probe_cache *pcache,