tools/perf: Backport fixes for 4.9 for newer toolchain
cover tools/perf: Backport fixes for 4.9 for newer toolchain
Hi,
While porting failed perf-probe fix to 4.9.y (*), I found the perf command had build errors on Ubuntu 20.04. Fortunately all issues has been fixed on the upstream. Thus I ported those patches too on 4.9.y.
(*) https://lore.kernel.org/stable/[email protected]/
I think perf tools in other stable branch also has similar issues.
Thank you,
---
Arnaldo Carvalho de Melo (1):
perf annotate: Use asprintf when formatting objdump command line
Changbin Du (1):
perf: Make perf able to build with latest libbfd
Jiri Olsa (1):
perf tools: Fix snprint warnings for gcc 8
Masami Hiramatsu (1):
perf probe: Fix to check blacklist address correctly
tools/perf/builtin-script.c | 24 ++++++++++++------------ tools/perf/tests/attr.c | 4 ++-- tools/perf/tests/pmu.c | 2 +- tools/perf/util/annotate.c | 17 ++++++++++++----- tools/perf/util/cgroup.c | 2 +- tools/perf/util/parse-events.c | 4 ++-- tools/perf/util/pmu.c | 2 +- tools/perf/util/probe-event.c | 21 +++++++++++++++------ tools/perf/util/srcline.c | 16 +++++++++++++++- 9 files changed, 61 insertions(+), 31 deletions(-)
--
Masami Hiramatsu (Linaro) <[email protected]>
PATCH 1/4 perf probe: Fix to check blacklist address correctly
commit 80526491c2ca6abc028c0f0dbb0707a1f35fb18a upstream.
Fix to check kprobe blacklist address correctly with relocated address by adjusting debuginfo address.
Since the address in the debuginfo is same as objdump, it is different from relocated kernel address with KASLR. Thus, 'perf probe' always misses to catch the blacklisted addresses.
Without this patch, 'perf probe' can not detect the blacklist addresses on a KASLR enabled kernel.
# perf probe kprobe_dispatcher
Failed to write event: Invalid argument
Error: Failed to add events.
#With this patch, it correctly shows the error message.
# perf probe kprobe_dispatcher
kprobe_dispatcher is blacklisted function, skip it.
Probe point 'kprobe_dispatcher' not found.
Error: Failed to add events.
#--- tools/perf/util/probe-event.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
tools/perf/util/probe-event.c +15 -6
@@ -118,7 +118,7 @@ static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)return machine__find_kernel_function (host_machine, addr, mapp); } -static struct ref_reloc_sym *kernel_get_ref_reloc_sym (void ) +static struct ref_reloc_sym *kernel_get_ref_reloc_sym (struct map **pmap) {/* kmap->ref_reloc_sym should be set if host_machine is initialized */ struct kmap *kmap;
@@ -130,6 +130,10 @@ static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void) kmap =map__kmap (map);if (!kmap)return NULL ; + +if (pmap) + *pmap = map; +return kmap->ref_reloc_sym; }
@@ -141,7 +145,7 @@ static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,struct map *map;/* ref_reloc_sym is just a label. Need a special fix*/ - reloc_sym =kernel_get_ref_reloc_sym (); + reloc_sym =kernel_get_ref_reloc_sym (NULL );if (reloc_sym &&strcmp (name, reloc_sym->name) ==0 ) *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;else {
@@ -742,6 +746,7 @@ post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, int ntevs) {struct ref_reloc_sym *reloc_sym; +struct map *map;char *tmp;int i, skipped =0 ;
@@ -750,7 +755,7 @@ post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,return post_process_offline_probe_trace_events (tevs, ntevs, symbol_conf.vmlinux_name); - reloc_sym =kernel_get_ref_reloc_sym (); + reloc_sym =kernel_get_ref_reloc_sym (&map);if (!reloc_sym) {pr_warning ("Relocated base symbol is not found!\n" );return -EINVAL ;
@@ -759,9 +764,13 @@ post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,for (i =0 ; i < ntevs; i++) {if (!tevs[i].point.address || tevs[i].point.retprobe)continue ; -/* If we found a wrong one, mark it by NULL symbol */ +/* +* If we found a wrong one, mark it by NULL symbol. +* Since addresses in debuginfo is same as objdump, we need +* to convert it to addresses on memory. +*/ if (kprobe_warn_out_range (tevs[i].point.symbol, - tevs[i].point.address)) { +map__objdump_2mem (map, tevs[i].point.address))) { tmp =NULL ; skipped++; }else {
@@ -2850,7 +2859,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,/* Note that the symbols in the kmodule are not relocated */ if (!pev->uprobes && !pp->retprobe && !pev->target) { - reloc_sym =kernel_get_ref_reloc_sym (); + reloc_sym =kernel_get_ref_reloc_sym (NULL );if (!reloc_sym) {pr_warning ("Relocated base symbol is not found!\n" ); ret = -EINVAL ;
PATCH 2/4 perf annotate: Use asprintf when formatting objdump command line
In reply to Masami HiramatsuFrom: Arnaldo Carvalho de Melo <[email protected]>
commit 6810158d526e483868e519befff407b91e76b3db upstream.
We were using a local buffer with an arbitrary size, that would have to get increased to avoid truncation as warned by gcc 8:
util/annotate.c: In function 'symbol__disassemble':
util/annotate.c:1488:4: error: '%s' directive output may be truncated writing up to 4095 bytes into a region of size between 3966 and 8086 [-Werror=format-truncation=]
"%s %s%s --start-address=0x%016" PRIx64
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/annotate.c:1498:20:
symfs_filename, symfs_filename);
~~~~~~~~~~~~~~
util/annotate.c:1490:50: note: format string is defined here
" -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
^~
In file included from /usr/include/stdio.h:861,
from util/color.h:5,
from util/sort.h:8,
from util/annotate.c:14:
/usr/include/bits/stdio2.h:67:10: note: '__builtin___snprintf_chk' output 116 or more bytes (assuming 8331) into a destination of size 8192
return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__bos (__s), __fmt, __va_arg_pack ());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~So switch to asprintf, that will make sure enough space is available.
--- tools/perf/util/annotate.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
tools/perf/util/annotate.c +12 -5
@@ -1302,7 +1302,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filint symbol__disassemble (struct symbol *sym,struct map *map,size_t privsize) {struct dso *dso = map->dso; -char command[PATH_MAX *2 ]; +char *command;FILE *file;char symfs_filename[PATH_MAX ];struct kcore_extract kce;
@@ -1364,7 +1364,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)strcpy (symfs_filename, tmp); } - snprintf(command, sizeof(command), + err = asprintf(&command, "%s %s%s --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
@@ -1377,12 +1377,17 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize) symbol_conf.annotate_src ?"-S" :"" , symfs_filename, symfs_filename); +if (err <0 ) { +pr_err ("Failure allocating memory for the command to run\n" ); + goto out_remove_tmp; + } +pr_debug ("Executing: %s\n" , command); err =-1 ;if (pipe (stdout_fd) <0 ) {pr_err ("Failure creating the pipe to run %s\n" , command); - goto out_remove_tmp; + goto out_free_command; } pid =fork ();
@@ -1409,7 +1414,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)* If we were using debug info should retry with * original binary. */ - goto out_remove_tmp; + goto out_free_command; } nline =0 ;
@@ -1432,6 +1437,8 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)fclose (file); err =0 ; +out_free_command: +free (command); out_remove_tmp:close (stdout_fd[0 ]);
@@ -1445,7 +1452,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize) out_close_stdout:close (stdout_fd[1 ]); - goto out_remove_tmp; + goto out_free_command; }static void insert_source_line (struct rb_root *root,struct source_line *src_line)
PATCH 3/4 perf tools: Fix snprint warnings for gcc 8
In reply to Masami HiramatsuFrom: Jiri Olsa <[email protected]>
commit 77f18153c080855e1c3fb520ca31a4e61530121d upstream.
[Add an additional sprintf replacement in tools/perf/builtin-script.c]
With gcc 8 we get new set of snprintf() warnings that breaks the compilation, one example:
tests/mem.c: In function ‘check’:
tests/mem.c:19:48: error: ‘%s’ directive output may be truncated writing \
up to 99 bytes into a region of size 89 [-Werror=format-truncation=]
snprintf(failure, sizeof failure, "unexpected %s", out);
The gcc docs says:
To avoid the warning either use a bigger buffer or handle the function's return value which indicates whether or not its output has been truncated.
Given that all these warnings are harmless, because the code either properly fails due to uncomplete file path or we don't care for truncated output at all, I'm changing all those snprintf() calls to scnprintf(), which actually 'checks' for the snprint return value so the gcc stays silent.
--- tools/perf/builtin-script.c | 24 ++++++++++++------------ tools/perf/tests/attr.c | 4 ++-- tools/perf/tests/pmu.c | 2 +- tools/perf/util/cgroup.c | 2 +- tools/perf/util/parse-events.c | 4 ++-- tools/perf/util/pmu.c | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-)
tools/perf/builtin-script.c +12 -12
@@ -1516,7 +1516,7 @@ static int is_directory(const char *base_path, const struct dirent *dent)char path[PATH_MAX ];struct stat st; -sprintf (path,"%s/%s" , base_path, dent->d_name); +scnprintf (path,PATH_MAX ,"%s/%s" , base_path, dent->d_name);if (stat (path, &st))return 0 ;
@@ -1702,8 +1702,8 @@ static int list_available_scripts(const struct option *opt __maybe_unused, }for_each_lang (scripts_path, scripts_dir, lang_dirent) { -snprintf (lang_path,MAXPATHLEN ,"%s/%s/bin" , scripts_path, - lang_dirent->d_name); +scnprintf (lang_path,MAXPATHLEN ,"%s/%s/bin" , scripts_path, + lang_dirent->d_name); lang_dir =opendir (lang_path);if (!lang_dir)continue ;
@@ -1712,8 +1712,8 @@ static int list_available_scripts(const struct option *opt __maybe_unused, script_root =get_script_root (script_dirent,REPORT_SUFFIX );if (script_root) { desc =script_desc__findnew (script_root); -snprintf (script_path,MAXPATHLEN ,"%s/%s" , - lang_path, script_dirent->d_name); +scnprintf (script_path,MAXPATHLEN ,"%s/%s" , + lang_path, script_dirent->d_name);read_script_info (desc, script_path);free (script_root); }
@@ -1749,7 +1749,7 @@ static int check_ev_match(char *dir_name, char *scriptname,int match, len;FILE *fp; -sprintf (filename,"%s/bin/%s-record" , dir_name, scriptname); +scnprintf (filename,MAXPATHLEN ,"%s/bin/%s-record" , dir_name, scriptname); fp =fopen (filename,"r" );if (!fp)
@@ -1825,8 +1825,8 @@ int find_scripts(char **scripts_array, char **scripts_path_array) }for_each_lang (scripts_path, scripts_dir, lang_dirent) { -snprintf (lang_path,MAXPATHLEN ,"%s/%s" , scripts_path, - lang_dirent->d_name); +scnprintf (lang_path,MAXPATHLEN ,"%s/%s" , scripts_path, + lang_dirent->d_name);#ifdef NO_LIBPERL if (strstr (lang_path,"perl" ))continue ;
@@ -1881,8 +1881,8 @@ static char *get_script_path(const char *script_root, const char *suffix)return NULL ;for_each_lang (scripts_path, scripts_dir, lang_dirent) { -snprintf (lang_path,MAXPATHLEN ,"%s/%s/bin" , scripts_path, - lang_dirent->d_name); +scnprintf (lang_path,MAXPATHLEN ,"%s/%s/bin" , scripts_path, + lang_dirent->d_name); lang_dir =opendir (lang_path);if (!lang_dir)continue ;
@@ -1893,8 +1893,8 @@ static char *get_script_path(const char *script_root, const char *suffix)free (__script_root);closedir (lang_dir);closedir (scripts_dir); -snprintf (script_path,MAXPATHLEN ,"%s/%s" , - lang_path, script_dirent->d_name); +scnprintf (script_path,MAXPATHLEN ,"%s/%s" , + lang_path, script_dirent->d_name);return strdup (script_path); }free (__script_root);
tools/perf/tests/attr.c +2 -2
@@ -147,8 +147,8 @@ static int run_dir(const char *d, const char *perf)if (verbose) vcnt++; -snprintf (cmd,3 *PATH_MAX ,PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s" , - d, d, perf, vcnt, v); +scnprintf (cmd,3 *PATH_MAX ,PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s" , + d, d, perf, vcnt, v);return system (cmd) ?TEST_FAIL :TEST_OK ; }
tools/perf/tests/pmu.c +1 -1
@@ -95,7 +95,7 @@ static char *test_format_dir_get(void)struct test_format *format = &test_formats[i];FILE *file; -snprintf (name,PATH_MAX ,"%s/%s" , dir, format->name); +scnprintf (name,PATH_MAX ,"%s/%s" , dir, format->name); file =fopen (name,"w" );if (!file)
tools/perf/util/cgroup.c +1 -1
@@ -64,7 +64,7 @@ static int open_cgroup(char *name)if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 ))return -1 ; -snprintf (path,PATH_MAX ,"%s/%s" , mnt, name); +scnprintf (path,PATH_MAX ,"%s/%s" , mnt, name); fd =open (path,O_RDONLY );if (fd ==-1 )
tools/perf/util/parse-events.c +2 -2
@@ -195,8 +195,8 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)for_each_event (sys_dirent, evt_dir, evt_dirent) { -snprintf (evt_path,MAXPATHLEN ,"%s/%s/id" , dir_path, - evt_dirent->d_name); +scnprintf (evt_path,MAXPATHLEN ,"%s/%s/id" , dir_path, + evt_dirent->d_name); fd =open (evt_path,O_RDONLY );if (fd <0 )continue ;
tools/perf/util/pmu.c +1 -1
@@ -325,7 +325,7 @@ static int pmu_aliases_parse(char *dir, struct list_head *head)if (pmu_alias_info_file (name))continue ; -snprintf (path,PATH_MAX ,"%s/%s" , dir, name); +scnprintf (path,PATH_MAX ,"%s/%s" , dir, name); file =fopen (path,"r" );if (!file) {
PATCH 4/4 perf: Make perf able to build with latest libbfd
In reply to Masami HiramatsuFrom: Changbin Du <[email protected]>
commit 0ada120c883d4f1f6aafd01cf0fbb10d8bbba015 upstream.
libbfd has changed the bfd_section_* macros to inline functions bfd_section_<field> since 2019-09-18. See below two commits:
o http://www.sourceware.org/ml/gdb-cvs/2019-09/msg00064.html
o https://www.sourceware.org/ml/gdb-cvs/2019-09/msg00072.html
This fix make perf able to build with both old and new libbfd.
--- tools/perf/util/srcline.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
tools/perf/util/srcline.c +15 -1
@@ -86,16 +86,30 @@ static void find_address_in_section(bfd *abfd, asection *section, void *data)bfd_vma pc, vma;bfd_size_type size;struct a2l_data *a2l = data; +flagword flags;if (a2l->found)return ; -if ((bfd_get_section_flags (abfd, section) &SEC_ALLOC ) ==0 ) +#ifdef bfd_get_section_flags + flags =bfd_get_section_flags (abfd, section); +#else + flags =bfd_section_flags (section); +#endif +if ((flags &SEC_ALLOC ) ==0 )return ; pc = a2l->addr; +#ifdef bfd_get_section_vma vma =bfd_get_section_vma (abfd, section); +#else + vma =bfd_section_vma (section); +#endif +#ifdef bfd_get_section_size size =bfd_get_section_size (section); +#else + size =bfd_section_size (section); +#endif if (pc < vma || pc >= vma + size)return ;
Greg KH
maintainerQuoted 11 lines · Masami Hiramatsu
Hi,
While porting failed perf-probe fix to 4.9.y (*), I found the perf command had build errors on Ubuntu 20.04. Fortunately all issues has been fixed on the upstream. Thus I ported those patches too on 4.9.y.
(*) https://lore.kernel.org/stable/[email protected]/
I think perf tools in other stable branch also has similar issues.
Thank you,
Sorry for the delay, this, and the 4.4.y series you sent are now queued up.
greg k-h