Cleanups and a fix
Arnaldo Carvalho de Melo
maintainerv3 cover Cleanups and a fix
From: Arnaldo Carvalho de Melo <[email protected]>
Hi,
Here are some cleanups, a refresh for a patch Ian sent and fell
thru the cracks, using calloc where applicable, constify some arrays and add a missing header that was making the build fail on some musl systems.
Thanks a lot,
- Arnaldo
v3: Address https://sashiko.dev review, removed a malloc->calloc conversion, flawed, and removed the changes to trace-event-scripting.c, that made the build fail in some situation, see the committer notes in the patch for more details.
v2: remove the commenting of gen_vmlinux in tools/perf/tests/make as noticed by Ian Rogers.
Arnaldo Carvalho de Melo (3):
perf bench: Constify tables
perf tools: Use calloc() were applicable
perf symbol: Add missing libgen.h include to get basename() prototype
Ian Rogers (1):
perf tools: Make more global variables static
tools/perf/arch/arm/util/auxtrace.c | 6 +- tools/perf/arch/common.c | 22 ++-- tools/perf/arch/powerpc/util/auxtrace.c | 1 + tools/perf/arch/sh/include/dwarf-regs-table.h | 2 +- tools/perf/arch/x86/tests/amd-ibs-period.c | 3 +- tools/perf/arch/x86/tests/dwarf-unwind.c | 11 +- tools/perf/arch/x86/util/pmu.c | 1 - tools/perf/bench/breakpoint.c | 4 +- tools/perf/bench/mem-functions.c | 2 +- tools/perf/bench/numa.c | 15 +-- tools/perf/bench/sched-messaging.c | 2 +- tools/perf/bench/uprobe.c | 2 +- tools/perf/builtin-annotate.c | 1 - tools/perf/builtin-bench.c | 42 +++---- tools/perf/builtin-c2c.c | 13 ++- tools/perf/builtin-config.c | 2 +- tools/perf/builtin-data.c | 8 +- tools/perf/builtin-diff.c | 6 +- tools/perf/builtin-ftrace.c | 1 + tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-kwork.c | 14 +-- tools/perf/builtin-record.c | 10 +- tools/perf/builtin-sched.c | 6 +- tools/perf/builtin-script.c | 10 +- tools/perf/builtin-stat.c | 2 +- tools/perf/builtin-top.c | 5 +- tools/perf/builtin-trace.c | 4 +- tools/perf/jvmti/libjvmti.c | 5 +- tools/perf/tests/bp_signal.c | 2 +- tools/perf/tests/code-reading.c | 1 + tools/perf/tests/dso-data.c | 2 +- tools/perf/tests/thread-map.c | 1 - tools/perf/tests/wp.c | 6 +- tools/perf/util/annotate-arch/annotate-x86.c | 1 + tools/perf/util/block-range.c | 2 +- tools/perf/util/bpf-event.c | 2 +- tools/perf/util/bpf_counter.c | 4 +- tools/perf/util/bpf_counter_cgroup.c | 1 - tools/perf/util/bpf_off_cpu.c | 2 +- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/data.c | 2 +- tools/perf/util/db-export.c | 1 - tools/perf/util/debug.c | 2 +- tools/perf/util/debuginfo.c | 19 ++-- tools/perf/util/disasm.c | 1 + tools/perf/util/event.c | 1 - tools/perf/util/evlist.c | 3 +- tools/perf/util/header.c | 18 +-- tools/perf/util/hist.c | 2 +- tools/perf/util/mem2node.c | 2 +- tools/perf/util/pmus.c | 2 +- tools/perf/util/powerpc-vpadtl.c | 1 + tools/perf/util/probe-event.c | 17 ++- tools/perf/util/probe-file.c | 2 +- tools/perf/util/probe-finder.c | 8 +- tools/perf/util/session.c | 2 +- tools/perf/util/sort.c | 104 +++++++++--------- tools/perf/util/srcline.c | 1 + tools/perf/util/stat-shadow.c | 1 - tools/perf/util/symbol.h | 1 + tools/perf/util/unwind-libunwind-local.c | 1 - tools/perf/util/util.c | 2 - tools/perf/util/values.c | 8 +- 63 files changed, 203 insertions(+), 226 deletions(-)
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH v3 1/4 perf tools: Make more global variables static
From: Ian Rogers <[email protected]>
make check will run sparse on the perf code base. A frequent warning is "warning: symbol '...' was not declared. Should it be static?" Go through and make global definitions without declarations static.
In some cases it is deliberate due to dlsym accessing the symbol, this change doesn't clean up the missing declarations for perf test suites.
Sometimes things can opportunistically be made const.
Making somethings static exposed unused functions warnings, so restructuring of ifdefs was necessary for that.
These changes reduce the size of the perf binary by 568 bytes.
Committer notes:
Refreshed the patch, the original one fell thru the cracks, updated the size reduction.
Remove the trace-event-scripting.c changes, break the build, noticed with container builds and with sashiko:
https://sashiko.dev/#/patchset/20260401215306.2152898-1-acme%40kernel.org
--- tools/perf/arch/common.c | 22 ++-- tools/perf/arch/sh/include/dwarf-regs-table.h | 2 +- tools/perf/bench/breakpoint.c | 4 +- tools/perf/bench/mem-functions.c | 2 +- tools/perf/bench/numa.c | 2 +- tools/perf/bench/uprobe.c | 2 +- tools/perf/builtin-c2c.c | 7 +- tools/perf/builtin-config.c | 2 +- tools/perf/builtin-data.c | 8 +- tools/perf/builtin-diff.c | 4 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-kwork.c | 12 +- tools/perf/builtin-script.c | 2 +- tools/perf/builtin-top.c | 5 +- tools/perf/tests/bp_signal.c | 2 +- tools/perf/tests/dso-data.c | 2 +- tools/perf/tests/wp.c | 6 +- tools/perf/util/block-range.c | 2 +- tools/perf/util/bpf_counter.c | 4 +- tools/perf/util/bpf_off_cpu.c | 2 +- tools/perf/util/debug.c | 2 +- tools/perf/util/debuginfo.c | 19 ++-- tools/perf/util/sort.c | 104 +++++++++--------- tools/perf/util/util.c | 2 - 24 files changed, 109 insertions(+), 112 deletions(-)
tools/perf/arch/common.c +11 -11
@@ -9,14 +9,14 @@#include "../util/debug.h" #include <linux/zalloc.h> -const char *const arc_triplets[] = { +static const char *const arc_triplets[] = {"arc-linux-" ,"arc-snps-linux-uclibc-" ,"arc-snps-linux-gnu-" ,NULL }; -const char *const arm_triplets[] = { +static const char *const arm_triplets[] = {"arm-eabi-" ,"arm-linux-androideabi-" ,"arm-unknown-linux-" ,
@@ -28,13 +28,13 @@ const char *const arm_triplets[] = {NULL }; -const char *const arm64_triplets[] = { +static const char *const arm64_triplets[] = {"aarch64-linux-android-" ,"aarch64-linux-gnu-" ,NULL }; -const char *const powerpc_triplets[] = { +static const char *const powerpc_triplets[] = {"powerpc-unknown-linux-gnu-" ,"powerpc-linux-gnu-" ,"powerpc64-unknown-linux-gnu-" ,
@@ -43,40 +43,40 @@ const char *const powerpc_triplets[] = {NULL }; -const char *const riscv32_triplets[] = { +static const char *const riscv32_triplets[] = {"riscv32-unknown-linux-gnu-" ,"riscv32-linux-android-" ,"riscv32-linux-gnu-" ,NULL }; -const char *const riscv64_triplets[] = { +static const char *const riscv64_triplets[] = {"riscv64-unknown-linux-gnu-" ,"riscv64-linux-android-" ,"riscv64-linux-gnu-" ,NULL }; -const char *const s390_triplets[] = { +static const char *const s390_triplets[] = {"s390-ibm-linux-" ,"s390x-linux-gnu-" ,NULL }; -const char *const sh_triplets[] = { +static const char *const sh_triplets[] = {"sh-unknown-linux-gnu-" ,"sh-linux-gnu-" ,NULL }; -const char *const sparc_triplets[] = { +static const char *const sparc_triplets[] = {"sparc-unknown-linux-gnu-" ,"sparc64-unknown-linux-gnu-" ,"sparc64-linux-gnu-" ,NULL }; -const char *const x86_triplets[] = { +static const char *const x86_triplets[] = {"x86_64-pc-linux-gnu-" ,"x86_64-unknown-linux-gnu-" ,"i686-pc-linux-gnu-" ,
@@ -90,7 +90,7 @@ const char *const x86_triplets[] = {NULL }; -const char *const mips_triplets[] = { +static const char *const mips_triplets[] = {"mips-unknown-linux-gnu-" ,"mipsel-linux-android-" ,"mips-linux-gnu-" ,
tools/perf/arch/sh/include/dwarf-regs-table.h +1 -1
@@ -2,7 +2,7 @@#ifdef DEFINE_DWARF_REGSTR_TABLE /* This is included in perf/util/dwarf-regs.c */ -const char *const sh_regstr_tbl[] = { +static const char *const sh_regstr_tbl[] = {"r0" ,"r1" , "r2",
tools/perf/bench/breakpoint.c +2 -2
@@ -16,7 +16,7 @@#include "bench.h" #include "futex.h" -struct { +static struct { unsigned int nbreakpoints; unsigned int nparallel; unsigned int nthreads;
@@ -173,7 +173,7 @@ int bench_breakpoint_thread(int argc, const char **argv)return 0 ; } -struct { +static struct {unsigned int npassive;unsigned int nactive; } enable_params = {
tools/perf/bench/mem-functions.c +1 -1
@@ -399,7 +399,7 @@ static void mem_free(struct bench_mem_info *info __maybe_unused, *dst = *src =NULL ; } -struct function memcpy_functions[] = { +static struct function memcpy_functions[] = { { .name ="default" , .desc ="Default memcpy() provided by glibc" , .fn.init = mem_alloc,
tools/perf/bench/numa.c +1 -1
@@ -166,7 +166,7 @@ static struct global_info *g = NULL;static int parse_cpus_opt (const struct option *opt,const char *arg,int unset);static int parse_nodes_opt (const struct option *opt,const char *arg,int unset); -struct params p0; +static struct params p0;static const struct option options[] = {OPT_INTEGER ('p' ,"nr_proc" , &p0.nr_proc,"number of processes" ),
tools/perf/bench/uprobe.c +1 -1
@@ -58,7 +58,7 @@ static const char * const bench_uprobe_usage[] = { goto cleanup; \ } -struct bench_uprobe_bpf *skel; +static struct bench_uprobe_bpf *skel;static int bench_uprobe__setup_bpf_skel (enum bench_uprobe bench) {
tools/perf/builtin-c2c.c +4 -3
@@ -2892,9 +2892,10 @@ static int ui_quirks(void)#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent" -const char callchain_help[] ="Display call graph (stack chain/backtrace):\n\n" -CALLCHAIN_REPORT_HELP -"\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT ; +static const char callchain_help[] = +"Display call graph (stack chain/backtrace):\n\n" +CALLCHAIN_REPORT_HELP +"\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT ;static int parse_callchain_opt (const struct option *opt,const char *arg,int unset)
tools/perf/builtin-config.c +1 -1
@@ -23,7 +23,7 @@ static const char * const config_usage[] = { NULL }; -enum actions { +static enum actions {ACTION_LIST =1 } actions;
tools/perf/builtin-data.c +4 -4
@@ -28,15 +28,15 @@ static const char *data_usage[] = {NULL }; -const char *to_json; -const char *to_ctf; -struct perf_data_convert_opts opts = { +static const char *to_json; +static const char *to_ctf; +static struct perf_data_convert_opts opts = { .force = false, .all = false, .time_str =NULL , }; -const struct option data_options[] = { +static const struct option data_options[] = {OPT_INCR ('v' ,"verbose" , &verbose,"be more verbose" ),OPT_STRING ('i' ,"input" , &input_name,"file" ,"input file name" ),OPT_STRING (0 ,"to-json" , &to_json,NULL ,"Convert to JSON format" ),
tools/perf/builtin-diff.c +2 -2
@@ -113,7 +113,7 @@ enum { COMPUTE_STREAM, /* After COMPUTE_MAX to avoid use current compute arrays */ }; -const char *compute_names[COMPUTE_MAX ] = { +static const char *compute_names[COMPUTE_MAX ] = { [COMPUTE_DELTA ] ="delta" , [COMPUTE_DELTA_ABS ] ="delta-abs" , [COMPUTE_RATIO ] ="ratio" ,
@@ -382,7 +382,7 @@ static void block_hist_free(void *he)free (bh); } -struct hist_entry_ops block_hist_ops = { +static struct hist_entry_ops block_hist_ops = { .new = block_hist_zalloc, .free = block_hist_free, };
tools/perf/builtin-kmem.c +1 -1
@@ -82,7 +82,7 @@ static unsigned long nr_allocs, nr_cross_allocs;/* filters for controlling start and stop of time of analysis */ static struct perf_time_interval ptime; -const char *time_str; +static const char *time_str;static int insert_alloc_stat (unsigned long call_site,unsigned long ptr,int bytes_req,int bytes_alloc,int cpu)
tools/perf/builtin-kwork.c +6 -6
@@ -985,7 +985,7 @@ static int process_irq_handler_exit_event(const struct perf_tool *tool,return 0 ; } -const struct evsel_str_handler irq_tp_handlers[] = { +static const struct evsel_str_handler irq_tp_handlers[] = { {"irq:irq_handler_entry" , process_irq_handler_entry_event, }, {"irq:irq_handler_exit" , process_irq_handler_exit_event, }, };
@@ -1080,7 +1080,7 @@ static int process_softirq_exit_event(const struct perf_tool *tool,return 0 ; } -const struct evsel_str_handler softirq_tp_handlers[] = { +static const struct evsel_str_handler softirq_tp_handlers[] = { {"irq:softirq_raise" , process_softirq_raise_event, }, {"irq:softirq_entry" , process_softirq_entry_event, }, {"irq:softirq_exit" , process_softirq_exit_event, },
@@ -1211,7 +1211,7 @@ static int process_workqueue_execute_end_event(const struct perf_tool *tool,return 0 ; } -const struct evsel_str_handler workqueue_tp_handlers[] = { +static const struct evsel_str_handler workqueue_tp_handlers[] = { {"workqueue:workqueue_activate_work" , process_workqueue_activate_work_event, }, {"workqueue:workqueue_execute_start" , process_workqueue_execute_start_event, }, {"workqueue:workqueue_execute_end" , process_workqueue_execute_end_event, },
@@ -1281,7 +1281,7 @@ static int process_sched_switch_event(const struct perf_tool *tool,return 0 ; } -const struct evsel_str_handler sched_tp_handlers[] = { +static const struct evsel_str_handler sched_tp_handlers[] = { {"sched:sched_switch" , process_sched_switch_event, }, };
@@ -1561,13 +1561,13 @@ static void print_bad_events(struct perf_kwork *kwork) } } -const char *graph_load ="||||||||||||||||||||||||||||||||||||||||||||||||" ; -const char *graph_idle =" " ;static void top_print_per_cpu_load (struct perf_kwork *kwork) {int i, load_width;u64 total, load, load_ratio;struct kwork_top_stat *stat = &kwork->top_stat; +const char *graph_load ="||||||||||||||||||||||||||||||||||||||||||||||||" ; +const char *graph_idle =" " ;for (i =0 ; i <MAX_NR_CPUS ; i++) { total = stat->cpus_runtime[i].total;
tools/perf/builtin-script.c +1 -1
@@ -166,7 +166,7 @@ struct perf_script {int range_num; }; -struct output_option { +static struct output_option {const char *str;enum perf_output_field field; } all_output_options[] = {
tools/perf/builtin-top.c +2 -3
@@ -1449,11 +1449,10 @@ parse_percent_limit(const struct option *opt, const char *arg,return 0 ; } -const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP -"\n\t\t\t\tDefault: fp,graph,0.5,caller,function" ; -int cmd_top (int argc,const char **argv) { + const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP +"\n\t\t\t\tDefault: fp,graph,0.5,caller,function" ;char errbuf[BUFSIZ ];struct perf_top top = { .count_filter =5 ,
tools/perf/tests/bp_signal.c +1 -1
@@ -36,7 +36,7 @@ static int fd3;static int overflows;static int overflows_2; -volatile long the_var; +static volatile long the_var;/*
tools/perf/tests/dso-data.c +1 -1
@@ -58,7 +58,7 @@ struct test_data_offset {int size; }; -struct test_data_offset offsets[] = { +static struct test_data_offset offsets[] = {/* Fill first cache page. */ { .offset =10 ,
tools/perf/tests/wp.c +3 -3
@@ -22,11 +22,11 @@ do { \#ifdef __i386__/* Only breakpoint length less-than 8 has hardware support on i386. */ -volatile u32 data1; +static volatile u32 data1;#else -volatile u64 data1; +static volatile u64 data1;#endif -volatile u8 data2[3 ]; +static volatile u8 data2[3 ];#ifndef __s390x__static int wp_read (int fd,long long *count,int size)
tools/perf/util/block-range.c +1 -1
@@ -4,7 +4,7 @@#include <assert.h> #include <stdlib.h> -struct { +static struct {struct rb_root root;u64 blocks; } block_ranges;
tools/perf/util/bpf_counter.c +2 -2
@@ -353,7 +353,7 @@ static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu_map_idxreturn 0 ; } -struct bpf_counter_ops bpf_program_profiler_ops = { +static struct bpf_counter_ops bpf_program_profiler_ops = { .load = bpf_program_profiler__load, .enable = bpf_program_profiler__enable, .disable = bpf_program_profiler__disable,
@@ -833,7 +833,7 @@ static int bperf__destroy(struct evsel *evsel)* the leader prog. */ -struct bpf_counter_ops bperf_ops = { +static struct bpf_counter_ops bperf_ops = { .load = bperf__load, .enable = bperf__enable, .disable = bperf__disable,
tools/perf/util/bpf_off_cpu.c +1 -1
@@ -39,7 +39,7 @@ union off_cpu_data {u64 array[1024 /sizeof (u64)]; }; -u64 off_cpu_raw[MAX_STACKS +5 ]; +static u64 off_cpu_raw[MAX_STACKS +5 ];static int off_cpu_config (struct evlist *evlist) {
tools/perf/util/debug.c +1 -1
@@ -48,7 +48,7 @@ int debug_ordered_events;static int redirect_to_stderr;int debug_data_convert;static FILE *_debug_file; -bool debug_display_time; +static bool debug_display_time;int debug_type_profile;FILE *debug_file (void )
tools/perf/util/debuginfo.c +9 -10
@@ -88,18 +88,17 @@ static struct debuginfo *__debuginfo__new(const char *path)return dbg; } -enum dso_binary_type distro_dwarf_types[] = { -DSO_BINARY_TYPE__FEDORA_DEBUGINFO , -DSO_BINARY_TYPE__UBUNTU_DEBUGINFO , -DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO , -DSO_BINARY_TYPE__BUILDID_DEBUGINFO , -DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO , -DSO_BINARY_TYPE__NOT_FOUND , -}; -struct debuginfo *debuginfo__new (const char *path) { -enum dso_binary_type *type; +const enum dso_binary_type distro_dwarf_types[] = { +DSO_BINARY_TYPE__FEDORA_DEBUGINFO , +DSO_BINARY_TYPE__UBUNTU_DEBUGINFO , +DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO , +DSO_BINARY_TYPE__BUILDID_DEBUGINFO , +DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO , +DSO_BINARY_TYPE__NOT_FOUND , + }; +const enum dso_binary_type *type;char buf[PATH_MAX ], nil ='\0' ;struct dso *dso;struct debuginfo *dinfo =NULL ;
tools/perf/util/sort.c +52 -52
399 lines not on this page · raw message
tools/perf/util/util.c +0 -2
@@ -77,8 +77,6 @@ bool sysctl__nmi_watchdog_enabled(void)return nmi_watchdog; } -bool test_attr__enabled; -bool exclude_GH_default;bool perf_host = true;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 1/4] perf tools: Make more global variables static
Quoted 18 lines · Arnaldo Carvalho de Melo
From: Ian Rogers <[email protected]>
make checkwill run sparse on the perf code base. A frequent warning is "warning: symbol '...' was not declared. Should it be static?" Go through and make global definitions without declarations static.In some cases it is deliberate due to dlsym accessing the symbol, this change doesn't clean up the missing declarations for perf test suites.
Sometimes things can opportunistically be made const.
Making somethings static exposed unused functions warnings, so restructuring of ifdefs was necessary for that.
These changes reduce the size of the perf binary by 568 bytes.
Committer notes:
Quoted 7 lines
Refreshed the patch, the original one fell thru the cracks, updated the size reduction.
Remove the
trace-event-scripting.cchanges, break the build, noticed with container builds and with sashiko:https://sashiko.dev/#/patchset/20260401215306.2152898-1-acme%40kernel.org
Addressing a Sashiko review comment:
---
Does moving this array into the function without the static keyword force unnecessary stack allocation and runtime initialization?
Since top_callchain_help[] is no longer static, the compiler might allocate space on the stack and copy the contents from .rodata every time the function is called. Using a pointer (const char *top_callchain_help) or keeping it static const would prevent this overhead.
---
⬢ [acme@toolbx perf-tools-next]$ cat before
text data bss dec hex filename
12249047 362081 40040 12651168 c10aa0 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ size ~/bin/perf
text data bss dec hex filename
12248983 362081 40040 12651104 c10a60 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ git diff
tools/perf/builtin-top.c +1 -1
@@ -1451,7 +1451,7 @@ parse_percent_limit(const struct option *opt, const char *arg,int cmd_top (int argc,const char **argv) { - const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP + static const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP"\n\t\t\t\tDefault: fp,graph,0.5,caller,function" ;char errbuf[BUFSIZ ];struct perf_top top = {
⬢ [acme@toolbx perf-tools-next]$
The second suggestion didn't make a difference, but lets keep it static as well to ensure that is the case.
---
Is this also creating a stack-allocated array that requires initialization on every call? Making distro_dwarf_types[] static const would ensure it stays entirely in .rodata with zero runtime overhead.
---
⬢ [acme@toolbx perf-tools-next]$ cat before
text data bss dec hex filename
12248983 362081 40040 12651104 c10a60 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ size ~/bin/perf
text data bss dec hex filename
12248983 362081 40040 12651104 c10a60 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ git diff
tools/perf/util/debuginfo.c +1 -1
@@ -90,7 +90,7 @@ static struct debuginfo *__debuginfo__new(const char *path)struct debuginfo *debuginfo__new (const char *path) { -const enum dso_binary_type distro_dwarf_types[] = { +static const enum dso_binary_type distro_dwarf_types[] = {DSO_BINARY_TYPE__FEDORA_DEBUGINFO ,DSO_BINARY_TYPE__UBUNTU_DEBUGINFO ,DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO ,
⬢ [acme@toolbx perf-tools-next]$
I'm adding these two changes and will resubmit, holler if you disagree. :-)
- Arnaldo
Arnaldo Carvalho de Melo
maintainerPATCH v3 2/4 perf bench: Constify tables
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Those tables and variables don't change, better capture this by explicitely using 'const'.
--- tools/perf/builtin-bench.c | 42 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
tools/perf/builtin-bench.c +21 -21
@@ -37,14 +37,14 @@ struct bench { };#ifdef HAVE_LIBNUMA_SUPPORT -static struct bench numa_benchmarks[] = { +static const struct bench numa_benchmarks[] = { {"mem" ,"Benchmark for NUMA workloads" , bench_numa }, {"all" ,"Run all NUMA benchmarks" ,NULL }, {NULL ,NULL ,NULL } };#endif -static struct bench sched_benchmarks[] = { +static const struct bench sched_benchmarks[] = { {"messaging" ,"Benchmark for scheduling and IPC" , bench_sched_messaging }, {"pipe" ,"Benchmark for pipe() between two processes" , bench_sched_pipe }, {"seccomp-notify" ,"Benchmark for seccomp user notify" , bench_sched_seccomp_notify},
@@ -52,7 +52,7 @@ static struct bench sched_benchmarks[] = { { NULL, NULL, NULL } }; -static struct bench syscall_benchmarks[] = { +static const struct bench syscall_benchmarks[] = { {"basic" ,"Benchmark for basic getppid(2) calls" , bench_syscall_basic }, {"getpgid" ,"Benchmark for getpgid(2) calls" , bench_syscall_getpgid }, {"fork" ,"Benchmark for fork(2) calls" , bench_syscall_fork },
@@ -61,7 +61,7 @@ static struct bench syscall_benchmarks[] = { { NULL, NULL, NULL }, }; -static struct bench mem_benchmarks[] = { +static const struct bench mem_benchmarks[] = { {"memcpy" ,"Benchmark for memcpy() functions" , bench_mem_memcpy }, {"memset" ,"Benchmark for memset() functions" , bench_mem_memset }, { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit },
@@ -70,7 +70,7 @@ static struct bench mem_benchmarks[] = { { NULL, NULL, NULL } }; -static struct bench futex_benchmarks[] = { +static const struct bench futex_benchmarks[] = { {"hash" ,"Benchmark for futex hash table" , bench_futex_hash }, {"wake" ,"Benchmark for futex wake calls" , bench_futex_wake }, {"wake-parallel" ,"Benchmark for parallel futex wake calls" , bench_futex_wake_parallel },
@@ -82,7 +82,7 @@ static struct bench futex_benchmarks[] = { };#ifdef HAVE_EVENTFD_SUPPORT -static struct bench epoll_benchmarks[] = { +static const struct bench epoll_benchmarks[] = { {"wait" ,"Benchmark epoll concurrent epoll_waits" , bench_epoll_wait }, {"ctl" ,"Benchmark epoll concurrent epoll_ctls" , bench_epoll_ctl }, { "all", "Run all futex benchmarks", NULL },
@@ -90,7 +90,7 @@ static struct bench epoll_benchmarks[] = { };#endif // HAVE_EVENTFD_SUPPORT -static struct bench internals_benchmarks[] = { +static const struct bench internals_benchmarks[] = { {"synthesize" ,"Benchmark perf event synthesis" , bench_synthesize }, {"kallsyms-parse" ,"Benchmark kallsyms parsing" , bench_kallsyms_parse }, {"inject-build-id" ,"Benchmark build-id injection" , bench_inject_build_id },
@@ -99,14 +99,14 @@ static struct bench internals_benchmarks[] = { { NULL, NULL, NULL } }; -static struct bench breakpoint_benchmarks[] = { +static const struct bench breakpoint_benchmarks[] = { {"thread" ,"Benchmark thread start/finish with breakpoints" , bench_breakpoint_thread}, {"enable" ,"Benchmark breakpoint enable/disable" , bench_breakpoint_enable}, {"all" ,"Run all breakpoint benchmarks" ,NULL }, {NULL ,NULL ,NULL }, }; -static struct bench uprobe_benchmarks[] = { +static const struct bench uprobe_benchmarks[] = { {"baseline" ,"Baseline libc usleep(1000) call" , bench_uprobe_baseline, }, {"empty" ,"Attach empty BPF prog to uprobe on usleep, system wide" , bench_uprobe_empty, }, {"trace_printk" ,"Attach trace_printk BPF prog to uprobe on usleep syswide" , bench_uprobe_trace_printk, },
@@ -116,12 +116,12 @@ static struct bench uprobe_benchmarks[] = { };struct collection { -const char *name; -const char *summary; -struct bench *benchmarks; +const char *name; +const char *summary; +const struct bench *benchmarks; }; -static struct collection collections[] = { +static const struct collection collections[] = { {"sched" ,"Scheduler and IPC benchmarks" , sched_benchmarks }, {"syscall" ,"System call benchmarks" , syscall_benchmarks }, {"mem" ,"Memory access benchmarks" , mem_benchmarks },
@@ -147,9 +147,9 @@ static struct collection collections[] = {#define for_each_bench (coll, bench) \ for (bench = coll->benchmarks; bench && bench->name; bench++) -static void dump_benchmarks (struct collection *coll) +static void dump_benchmarks (const struct collection *coll) { -struct bench *bench; +const struct bench *bench;printf ("\n # List of available benchmarks for collection '%s':\n\n" , coll->name);
@@ -178,7 +178,7 @@ static const char * const bench_usage[] = {static void print_usage (void ) { -struct collection *coll; +const struct collection *coll;int i;printf ("Usage: \n" );
@@ -234,9 +234,9 @@ static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t freturn ret; } -static void run_collection (struct collection *coll) +static void run_collection (const struct collection *coll) { -struct bench *bench; +const struct bench *bench;const char *argv[2 ]; argv[1 ] =NULL ;
@@ -260,7 +260,7 @@ static void run_collection(struct collection *coll)static void run_all_collections (void ) { -struct collection *coll; +const struct collection *coll;for_each_collection (coll)run_collection (coll);
@@ -268,7 +268,7 @@ static void run_all_collections(void)int cmd_bench (int argc,const char **argv) { -struct collection *coll; +const struct collection *coll;int ret =0 ;/* Unbuffered output */
@@ -306,7 +306,7 @@ int cmd_bench(int argc, const char **argv) }for_each_collection (coll) { -struct bench *bench; +const struct bench *bench;if (strcmp (coll->name, argv[0 ]))continue ;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH v3 3/4 perf tools: Use calloc() were applicable
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Instead of using zalloc(nr_entries * sizeof_entry) that is what calloc() does.
In some places where linux/zalloc.h isn't needed, remove it, add when needed and was getting it indirectly.
--- tools/perf/arch/arm/util/auxtrace.c | 6 +++--- tools/perf/arch/powerpc/util/auxtrace.c | 1 + tools/perf/arch/x86/tests/amd-ibs-period.c | 3 +-- tools/perf/arch/x86/tests/dwarf-unwind.c | 11 +---------- tools/perf/arch/x86/util/pmu.c | 1 - tools/perf/bench/numa.c | 13 ++++--------- tools/perf/bench/sched-messaging.c | 2 +- tools/perf/builtin-annotate.c | 1 - tools/perf/builtin-c2c.c | 6 +++--- tools/perf/builtin-diff.c | 2 +- tools/perf/builtin-ftrace.c | 1 + tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-record.c | 10 +++++----- tools/perf/builtin-sched.c | 6 +++--- tools/perf/builtin-script.c | 8 ++++---- tools/perf/builtin-stat.c | 2 +- tools/perf/builtin-trace.c | 4 +--- tools/perf/jvmti/libjvmti.c | 5 ++--- tools/perf/tests/code-reading.c | 1 + tools/perf/tests/thread-map.c | 1 - tools/perf/util/annotate-arch/annotate-x86.c | 1 + tools/perf/util/bpf-event.c | 2 +- tools/perf/util/bpf_counter_cgroup.c | 1 - tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/data.c | 2 +- tools/perf/util/db-export.c | 1 - tools/perf/util/disasm.c | 1 + tools/perf/util/event.c | 1 - tools/perf/util/evlist.c | 3 +-- tools/perf/util/header.c | 18 +++++++++--------- tools/perf/util/hist.c | 2 +- tools/perf/util/mem2node.c | 2 +- tools/perf/util/pmus.c | 2 +- tools/perf/util/powerpc-vpadtl.c | 1 + tools/perf/util/probe-event.c | 17 ++++++++--------- tools/perf/util/probe-file.c | 2 +- tools/perf/util/probe-finder.c | 8 ++++---- tools/perf/util/session.c | 2 +- tools/perf/util/srcline.c | 1 + tools/perf/util/stat-shadow.c | 1 - tools/perf/util/unwind-libunwind-local.c | 1 - tools/perf/util/values.c | 8 ++++---- 42 files changed, 72 insertions(+), 93 deletions(-)
tools/perf/arch/arm/util/auxtrace.c +3 -3
@@ -8,7 +8,7 @@#include <errno.h> #include <stdbool.h> #include <linux/coresight-pmu.h> -#include <linux/zalloc.h> +#include <stdlib.h> #include <api/fs/fs.h> #include "../../../util/auxtrace.h"
@@ -27,7 +27,7 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err)/* arm_spe_xxxxxxxxx\0 */ char arm_spe_pmu_name[sizeof (ARM_SPE_PMU_NAME ) +10 ]; - arm_spe_pmus =zalloc (sizeof (struct perf_pmu *) * nr_cpus); + arm_spe_pmus =calloc (nr_cpus,sizeof (struct perf_pmu *));if (!arm_spe_pmus) {pr_err ("spes alloc failed\n" ); *err = -ENOMEM ;
@@ -79,7 +79,7 @@ static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err)if (!(*nr_ptts)) goto out; - hisi_ptt_pmus =zalloc (sizeof (struct perf_pmu *) * (*nr_ptts)); + hisi_ptt_pmus =calloc ((*nr_ptts),sizeof (struct perf_pmu *));if (!hisi_ptt_pmus) {pr_err ("hisi_ptt alloc failed\n" ); *err = -ENOMEM ;
tools/perf/arch/powerpc/util/auxtrace.c +1 -0
@@ -6,6 +6,7 @@#include <linux/kernel.h> #include <linux/types.h> #include <linux/string.h> +#include <linux/zalloc.h> #include "../../util/evlist.h" #include "../../util/debug.h"
tools/perf/arch/x86/tests/amd-ibs-period.c +1 -2
@@ -8,7 +8,6 @@#include "arch-tests.h" #include "linux/perf_event.h" -#include "linux/zalloc.h" #include "tests/tests.h" #include "../perf-sys.h" #include "pmu.h"
@@ -60,7 +59,7 @@ static int dummy_workload_1(unsigned long count) 0xcc, /* int 3 */ }; - p =zalloc (2 * page_size); + p =calloc (2 , page_size);if (!p) {printf ("malloc() failed. %m" );return 1 ;
tools/perf/arch/x86/tests/dwarf-unwind.c +1 -10
@@ -54,22 +54,13 @@ int test__arch_unwind_sample(struct perf_sample *sample, struct thread *thread) {struct regs_dump *regs =perf_sample__user_regs (sample); -u64 *buf; +u64 *buf =calloc (PERF_REGS_MAX ,sizeof (u64)); - buf =malloc (sizeof (u64) *PERF_REGS_MAX );if (!buf) {pr_debug ("failed to allocate sample uregs data\n" );return -1 ; } -#ifdef MEMORY_SANITIZER -/* -* Assignments to buf in the assembly function perf_regs_load aren't -* seen by memory sanitizer. Zero the memory to convince memory -* sanitizer the memory is initialized. -*/ -memset (buf,0 ,sizeof (u64) *PERF_REGS_MAX ); -#endif perf_regs_load (buf); regs->abi =PERF_SAMPLE_REGS_ABI ; regs->regs = buf;
tools/perf/arch/x86/util/pmu.c +0 -1
@@ -7,7 +7,6 @@#include <linux/stddef.h> #include <linux/string.h> #include <linux/perf_event.h> -#include <linux/zalloc.h> #include <api/fs/fs.h> #include <api/io_dir.h> #include <internal/cpumap.h>
tools/perf/bench/numa.c +4 -9
@@ -32,7 +32,6 @@#include <linux/kernel.h> #include <linux/time64.h> #include <linux/numa.h> -#include <linux/zalloc.h> #include "../util/header.h" #include "../util/mutex.h"
@@ -980,10 +979,8 @@ static int count_process_nodes(int process_nr)int nodes;int n, t; - node_present = (char *)malloc (g->p.nr_nodes *sizeof (char )); + node_present =calloc (g->p.nr_nodes,sizeof (char ));BUG_ON (!node_present); -for (nodes =0 ; nodes < g->p.nr_nodes; nodes++) - node_present[nodes] =0 ;for (t =0 ; t < g->p.nr_threads; t++) {struct thread_data *td;
@@ -1090,10 +1087,8 @@ static void calc_convergence(double runtime_ns_max, double *convergence)if (!g->p.show_convergence && !g->p.measure_convergence)return ; - nodes = (int *)malloc (g->p.nr_nodes *sizeof (int )); + nodes =calloc (g->p.nr_nodes,sizeof (int ));BUG_ON (!nodes); -for (node =0 ; node < g->p.nr_nodes; node++) - nodes[node] =0 ; loops_done_min =-1 ; loops_done_max =0 ;
@@ -1423,7 +1418,7 @@ static void worker_process(int process_nr)bind_to_memnode (td->bind_node);bind_to_cpumask (td->bind_cpumask); - pthreads =zalloc (g->p.nr_threads *sizeof (pthread_t)); + pthreads =calloc (g->p.nr_threads,sizeof (pthread_t)); process_data =setup_private_data (g->p.bytes_process);if (g->p.show_details >=3 ) {
@@ -1629,7 +1624,7 @@ static int __bench_numa(const char *name)if (init ())return -1 ; - pids =zalloc (g->p.nr_proc *sizeof (*pids)); + pids =calloc (g->p.nr_proc,sizeof (*pids)); pid =-1 ;if (g->p.serialize_startup) {
tools/perf/bench/sched-messaging.c +1 -1
@@ -301,7 +301,7 @@ int bench_sched_messaging(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_sched_message_usage,0 ); - worker_tab =malloc (num_fds *2 * num_groups *sizeof (union messaging_worker )); + worker_tab =calloc (num_fds *2 * num_groups,sizeof (union messaging_worker ));if (!worker_tab)err (EXIT_FAILURE ,"main:malloc()" );
tools/perf/builtin-annotate.c +0 -1
@@ -13,7 +13,6 @@#include <linux/list.h> #include "util/cache.h" #include <linux/rbtree.h> -#include <linux/zalloc.h> #include "util/symbol.h" #include "util/debug.h"
tools/perf/builtin-c2c.c +3 -3
@@ -155,7 +155,7 @@ static void *c2c_he_zalloc(size_t size)if (!c2c_he->nodeset) goto out_free; - c2c_he->node_stats =zalloc (c2c.nodes_cnt *sizeof (*c2c_he->node_stats)); + c2c_he->node_stats =calloc (c2c.nodes_cnt,sizeof (*c2c_he->node_stats));if (!c2c_he->node_stats) goto out_free;
@@ -2324,13 +2324,13 @@ static int setup_nodes(struct perf_session *session)if (!n)return -EINVAL ; - nodes =zalloc (sizeof (unsigned long *) * c2c.nodes_cnt); + nodes =calloc (c2c.nodes_cnt,sizeof (unsigned long *));if (!nodes)return -ENOMEM ; c2c.nodes = nodes; - cpu2node =zalloc (sizeof (int ) * c2c.cpus_cnt); + cpu2node =calloc (c2c.cpus_cnt,sizeof (int ));if (!cpu2node)return -ENOMEM ;
tools/perf/builtin-diff.c +1 -1
@@ -1891,7 +1891,7 @@ static int data_init(int argc, const char **argv)return -EINVAL ; } - data__files =zalloc (sizeof (*data__files) * data__files_cnt); + data__files =calloc (data__files_cnt,sizeof (*data__files));if (!data__files)return -ENOMEM ;
tools/perf/builtin-ftrace.c +1 -0
@@ -20,6 +20,7 @@#include <linux/capability.h> #include <linux/err.h> #include <linux/string.h> +#include <linux/zalloc.h> #include <sys/stat.h> #include "debug.h"
tools/perf/builtin-kwork.c +1 -1
@@ -2208,7 +2208,7 @@ static int perf_kwork__top(struct perf_kwork *kwork)struct __top_cpus_runtime *cpus_runtime;int ret =0 ; - cpus_runtime =zalloc (sizeof (struct __top_cpus_runtime ) * (MAX_NR_CPUS +1 )); + cpus_runtime =calloc (MAX_NR_CPUS +1 ,sizeof (struct __top_cpus_runtime ));if (!cpus_runtime)return -1 ;
tools/perf/builtin-record.c +5 -5
@@ -1070,12 +1070,12 @@ static int record__thread_data_init_maps(struct record_thread *thread_data, stru thread_data->nr_mmaps =bitmap_weight (thread_data->mask->maps.bits, thread_data->mask->maps.nbits);if (mmap) { - thread_data->maps =zalloc (thread_data->nr_mmaps *sizeof (struct mmap *)); + thread_data->maps =calloc (thread_data->nr_mmaps,sizeof (struct mmap *));if (!thread_data->maps)return -ENOMEM ; }if (overwrite_mmap) { - thread_data->overwrite_maps =zalloc (thread_data->nr_mmaps *sizeof (struct mmap *)); + thread_data->overwrite_maps =calloc (thread_data->nr_mmaps,sizeof (struct mmap *));if (!thread_data->overwrite_maps) {zfree (&thread_data->maps);return -ENOMEM ;
@@ -1220,7 +1220,7 @@ static int record__alloc_thread_data(struct record *rec, struct evlist *evlist)int t, ret;struct record_thread *thread_data; - rec->thread_data =zalloc (rec->nr_threads *sizeof (*(rec->thread_data))); + rec->thread_data =calloc (rec->nr_threads,sizeof (*(rec->thread_data)));if (!rec->thread_data) {pr_err ("Failed to allocate thread data\n" );return -ENOMEM ;
@@ -3710,7 +3710,7 @@ static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr {int t, ret; - rec->thread_masks =zalloc (nr_threads *sizeof (*(rec->thread_masks))); + rec->thread_masks =calloc (nr_threads,sizeof (*(rec->thread_masks)));if (!rec->thread_masks) {pr_err ("Failed to allocate thread masks\n" );return -ENOMEM ;
@@ -3920,7 +3920,7 @@ static int record__init_thread_numa_masks(struct record *rec, struct perf_cpu_mareturn -ENOMEM ; } - spec =zalloc (topo->nr *sizeof (char *)); + spec =calloc (topo->nr,sizeof (char *));if (!spec) {pr_err ("Failed to allocate NUMA spec\n" ); ret = -ENOMEM ;
tools/perf/builtin-sched.c +3 -3
@@ -2405,7 +2405,7 @@ static int init_idle_threads(int ncpu) {int i, ret; - idle_threads =zalloc (ncpu *sizeof (struct thread *)); + idle_threads =calloc (ncpu,sizeof (struct thread *));if (!idle_threads)return -ENOMEM ;
@@ -3483,7 +3483,7 @@ static int setup_cpus_switch_event(struct perf_sched *sched)if (!sched->cpu_last_switched)return -1 ; - sched->curr_pid =malloc (MAX_CPUS *sizeof (*(sched->curr_pid))); + sched->curr_pid =calloc (MAX_CPUS ,sizeof (*(sched->curr_pid)));if (!sched->curr_pid) {zfree (&sched->cpu_last_switched);return -1 ;
@@ -3559,7 +3559,7 @@ static int setup_map_cpus(struct perf_sched *sched) sched->max_cpu.cpu =sysconf (_SC_NPROCESSORS_CONF);if (sched->map.comp) { - sched->map.comp_cpus =zalloc (sched->max_cpu.cpu *sizeof (int )); + sched->map.comp_cpus =calloc (sched->max_cpu.cpu,sizeof (int ));if (!sched->map.comp_cpus)return -1 ; }
tools/perf/builtin-script.c +4 -4
@@ -3819,7 +3819,7 @@ static int has_required_arg(char *script_path)static int have_cmd (int argc,const char **argv) { -char **__argv =malloc (sizeof (const char *) * argc); +char **__argv =calloc (argc,sizeof (const char *));if (!__argv) {pr_err ("malloc failed\n" );
@@ -4317,7 +4317,7 @@ int cmd_script(int argc, const char **argv) } } - __argv =malloc ((argc +6 ) *sizeof (const char *)); + __argv =calloc (argc +6 ,sizeof (const char *));if (!__argv) {pr_err ("malloc failed\n" ); err = -ENOMEM ;
@@ -4343,7 +4343,7 @@ int cmd_script(int argc, const char **argv)dup2 (live_pipe[0 ],0 );close (live_pipe[1 ]); - __argv =malloc ((argc +4 ) *sizeof (const char *)); + __argv =calloc (argc +4 ,sizeof (const char *));if (!__argv) {pr_err ("malloc failed\n" ); err = -ENOMEM ;
@@ -4381,7 +4381,7 @@ int cmd_script(int argc, const char **argv) } } - __argv =malloc ((argc +2 ) *sizeof (const char *)); + __argv =calloc (argc +2 ,sizeof (const char *));if (!__argv) {pr_err ("malloc failed\n" ); err = -ENOMEM ;
tools/perf/builtin-stat.c +1 -1
@@ -2766,7 +2766,7 @@ int cmd_stat(int argc, const char **argv) }if (stat_config.walltime_run_table) { - stat_config.walltime_run =zalloc (stat_config.run_count *sizeof (stat_config.walltime_run[0 ])); + stat_config.walltime_run =calloc (stat_config.run_count,sizeof (stat_config.walltime_run[0 ]));if (!stat_config.walltime_run) {pr_err ("failed to setup -r option" ); goto out;
tools/perf/builtin-trace.c +1 -3
@@ -2265,9 +2265,7 @@ static int trace__validate_ev_qualifier(struct trace *trace)struct str_node *pos;size_t nr_used =0 , nr_allocated =strlist__nr_entries (trace->ev_qualifier); - trace->ev_qualifier_ids.entries =malloc (nr_allocated * -sizeof (trace->ev_qualifier_ids.entries[0 ])); - + trace->ev_qualifier_ids.entries =calloc (nr_allocated,sizeof (trace->ev_qualifier_ids.entries[0 ]));if (trace->ev_qualifier_ids.entries ==NULL ) {fputs ("Error:\tNot enough memory for allocating events qualifier ids\n" , trace->output);
tools/perf/jvmti/libjvmti.c +2 -3
@@ -98,7 +98,7 @@ get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **/* * Phase 2 -- allocate big enough line table */ - *tab =malloc (nr_total *sizeof (**tab)); + *tab =calloc (nr_total,sizeof (**tab));if (!*tab)return JVMTI_ERROR_OUT_OF_MEMORY ;
@@ -262,11 +262,10 @@ compiled_method_load_cb(jvmtiEnv *jvmti, } nr_lines =0 ; }else if (nr_lines >0 ) { - line_file_names =malloc (sizeof (char *) * nr_lines); + line_file_names =calloc (nr_lines,sizeof (char *));if (!line_file_names) {warnx ("jvmti: cannot allocate space for line table method names" ); }else { -memset (line_file_names,0 ,sizeof (char *) * nr_lines); ret =fill_source_filenames (jvmti, nr_lines, line_tab, line_file_names);if (ret !=JVMTI_ERROR_NONE ) {warnx ("jvmti: fill_source_filenames failed" );
tools/perf/tests/code-reading.c +1 -0
@@ -4,6 +4,7 @@#include <linux/kernel.h> #include <linux/rbtree.h> #include <linux/types.h> +#include <linux/zalloc.h> #include <inttypes.h> #include <stdlib.h> #include <unistd.h>
tools/perf/tests/thread-map.c +0 -1
@@ -9,7 +9,6 @@#include "debug.h" #include "event.h" #include "util/synthetic-events.h" -#include <linux/zalloc.h> #include <perf/event.h> #include <internal/threadmap.h>
tools/perf/util/annotate-arch/annotate-x86.c +1 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <string.h> #include <linux/compiler.h> +#include <linux/zalloc.h> #include <assert.h> #include <inttypes.h> #include "../annotate-data.h"
tools/perf/util/bpf-event.c +1 -1
@@ -349,7 +349,7 @@ static struct bpf_metadata *bpf_metadata_alloc(__u32 nr_prog_tags,if (!metadata)return NULL ; - metadata->prog_names =zalloc (nr_prog_tags *sizeof (char *)); + metadata->prog_names =calloc (nr_prog_tags,sizeof (char *));if (!metadata->prog_names) {bpf_metadata_free (metadata);return NULL ;
tools/perf/util/bpf_counter_cgroup.c +0 -1
@@ -11,7 +11,6 @@#include <sys/time.h> #include <sys/resource.h> #include <linux/err.h> -#include <linux/zalloc.h> #include <linux/perf_event.h> #include <api/fs/fs.h> #include <bpf/bpf.h>
tools/perf/util/data-convert-bt.c +1 -1
@@ -1360,7 +1360,7 @@ static int setup_streams(struct ctf_writer *cw, struct perf_session *session)*/ ncpus = env->nr_cpus_avail ?:MAX_CPUS ; - stream =zalloc (sizeof (*stream) * ncpus); + stream =calloc (ncpus,sizeof (*stream));if (!stream) {pr_err ("Failed to allocate streams.\n" );return -ENOMEM ;
tools/perf/util/data.c +1 -1
@@ -43,7 +43,7 @@ int perf_data__create_dir(struct perf_data *data, int nr)if (WARN_ON (!data->is_dir))return -EINVAL ; - files =zalloc (nr *sizeof (*files)); + files =calloc (nr,sizeof (*files));if (!files)return -ENOMEM ;
tools/perf/util/db-export.c +0 -1
@@ -19,7 +19,6 @@#include "callchain.h" #include "call-path.h" #include "db-export.h" -#include <linux/zalloc.h> int db_export__init (struct db_export *dbe) {
tools/perf/util/disasm.c +1 -0
@@ -13,6 +13,7 @@#include <unistd.h> #include <linux/string.h> +#include <linux/zalloc.h> #include <subcmd/run-command.h> #include "annotate.h"
tools/perf/util/event.c +0 -1
@@ -12,7 +12,6 @@#include <unistd.h> #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ #include <linux/perf_event.h> -#include <linux/zalloc.h> #include "cpumap.h" #include "dso.h" #include "event.h"
tools/perf/util/evlist.c +1 -2
@@ -825,9 +825,8 @@ static struct mmap *evlist__alloc_mmap(struct evlist *evlist, bool overwrite) {int i; -struct mmap *map; +struct mmap *map =calloc (evlist->core.nr_mmaps,sizeof (struct mmap )); - map =zalloc (evlist->core.nr_mmaps *sizeof (struct mmap ));if (!map)return NULL ;
tools/perf/util/header.c +9 -9
@@ -2761,7 +2761,7 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)if (!cmdline)return -1 ; - argv =zalloc (sizeof (char *) * (nr +1 )); + argv =calloc (nr +1 ,sizeof (char *));if (!argv) goto error;
@@ -2915,7 +2915,7 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)if (do_read_u32 (ff, &nr))return -1 ; - nodes =zalloc (sizeof (*nodes) * nr); + nodes =calloc (nr,sizeof (*nodes));if (!nodes)return -ENOMEM ;
@@ -3113,7 +3113,7 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)if (do_read_u32 (ff, &cnt))return -1 ; - caches =zalloc (sizeof (*caches) * cnt); + caches =calloc (cnt,sizeof (*caches));if (!caches)return -1 ;
@@ -3195,7 +3195,7 @@ static int process_mem_topology(struct feat_fd *ff,if (do_read_u64 (ff, &nr))return -1 ; - nodes =zalloc (sizeof (*nodes) * nr); + nodes =calloc (nr,sizeof (*nodes));if (!nodes)return -1 ;
@@ -3285,7 +3285,7 @@ static int process_hybrid_topology(struct feat_fd *ff,if (do_read_u32 (ff, &nr))return -1 ; - nodes =zalloc (sizeof (*nodes) * nr); + nodes =calloc (nr,sizeof (*nodes));if (!nodes)return -ENOMEM ;
@@ -3492,7 +3492,7 @@ static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,if (!nr_pmu_caps)return 0 ; - *caps =zalloc (sizeof (char *) * nr_pmu_caps); + *caps =calloc (nr_pmu_caps,sizeof (char *));if (!*caps)return -1 ;
@@ -3569,7 +3569,7 @@ static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)return 0 ; } - pmu_caps =zalloc (sizeof (*pmu_caps) * nr_pmu); + pmu_caps =calloc (nr_pmu,sizeof (*pmu_caps));if (!pmu_caps)return -ENOMEM ;
@@ -3622,7 +3622,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused nra = env->nr_cpus_avail; nr = env->nr_cpus_online; - cd_map =zalloc (sizeof (*cd_map) * nra); + cd_map =calloc (nra,sizeof (*cd_map));if (!cd_map)return -1 ;
@@ -3655,7 +3655,7 @@ static int process_cpu_domain_info(struct feat_fd *ff, void *data __maybe_unused cd_map[cpu]->nr_domains = nr_domains; - cd_map[cpu]->domains =zalloc (sizeof (*d_info) * max_sched_domains); + cd_map[cpu]->domains =calloc (max_sched_domains,sizeof (*d_info));if (!cd_map[cpu]->domains)return -1 ;
tools/perf/util/hist.c +1 -1
@@ -1151,7 +1151,7 @@ iter_prepare_cumulative_entry(struct hist_entry_iter *iter,* cumulated only one time to prevent entries more than 100% * overhead. */ - he_cache =malloc (sizeof (*he_cache) * (cursor->nr +1 )); + he_cache =calloc (cursor->nr +1 ,sizeof (*he_cache));if (he_cache ==NULL )return -ENOMEM ;
tools/perf/util/mem2node.c +1 -1
@@ -59,7 +59,7 @@ int mem2node__init(struct mem2node *map, struct perf_env *env) max +=bitmap_weight (n->set, n->size); } - entries =zalloc (sizeof (*entries) * max); + entries =calloc (max,sizeof (*entries));if (!entries)return -ENOMEM ;
tools/perf/util/pmus.c +1 -1
@@ -621,7 +621,7 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *pwhile ((pmu =scan_fn (pmu)) !=NULL ) len +=perf_pmu__num_events (pmu); - aliases =zalloc (sizeof (struct sevent ) * len); + aliases =calloc (len,sizeof (struct sevent ));if (!aliases) {pr_err ("FATAL: not enough memory to print PMU events\n" );return ;
tools/perf/util/powerpc-vpadtl.c +1 -0
@@ -4,6 +4,7 @@*/ #include <linux/string.h> +#include <linux/zalloc.h> #include <errno.h> #include <inttypes.h> #include "color.h"
tools/perf/util/probe-event.c +8 -9
@@ -1850,7 +1850,7 @@ int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)/* Copy arguments and ensure return probe has no C argument */ pev->nargs = argc -1 ; - pev->args =zalloc (sizeof (struct perf_probe_arg ) * pev->nargs); + pev->args =calloc (pev->nargs,sizeof (struct perf_probe_arg ));if (pev->args ==NULL ) { ret = -ENOMEM ; goto out;
@@ -2000,7 +2000,7 @@ int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) } tev->nargs = argc -2 ; - tev->args =zalloc (sizeof (struct probe_trace_arg ) * tev->nargs); + tev->args =calloc (tev->nargs,sizeof (struct probe_trace_arg ));if (tev->args ==NULL ) { ret = -ENOMEM ; goto out;
@@ -2373,7 +2373,7 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,/* Convert trace_arg to probe_arg */ pev->nargs = tev->nargs; - pev->args =zalloc (sizeof (struct perf_probe_arg ) * pev->nargs); + pev->args =calloc (pev->nargs,sizeof (struct perf_probe_arg ));if (pev->args ==NULL )return -ENOMEM ;for (i =0 ; i < tev->nargs && ret >=0 ; i++) {
@@ -2480,7 +2480,7 @@ int perf_probe_event__copy(struct perf_probe_event *dst,if (perf_probe_point__copy (&dst->point, &src->point) <0 ) goto out_err; - dst->args =zalloc (sizeof (struct perf_probe_arg ) * src->nargs); + dst->args =calloc (src->nargs,sizeof (struct perf_probe_arg ));if (!dst->args) goto out_err; dst->nargs = src->nargs;
@@ -3179,7 +3179,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev, }/* Setup result trace-probe-events */ - *tevs =zalloc (sizeof (*tev) * num_matched_functions); + *tevs =calloc (num_matched_functions,sizeof (*tev));if (!*tevs) { ret = -ENOMEM ; goto out;
@@ -3251,8 +3251,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev, tev->uprobes = pev->uprobes; tev->nargs = pev->nargs;if (tev->nargs) { - tev->args =zalloc (sizeof (struct probe_trace_arg ) * - tev->nargs); + tev->args =calloc (tev->nargs,sizeof (struct probe_trace_arg ));if (tev->args ==NULL ) goto nomem_out; }
@@ -3363,7 +3362,7 @@ static int try_to_find_absolute_address(struct perf_probe_event *pev, } tev->nargs = pev->nargs; - tev->args =zalloc (sizeof (struct probe_trace_arg ) * tev->nargs); + tev->args =calloc (tev->nargs,sizeof (struct probe_trace_arg ));if (!tev->args) goto errout;
@@ -3549,7 +3548,7 @@ static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, goto out; } - *tevs =zalloc (ret *sizeof (*tev)); + *tevs =calloc (ret,sizeof (*tev));if (!*tevs) { ret = -ENOMEM ; goto out;
tools/perf/util/probe-file.c +1 -1
@@ -414,7 +414,7 @@ int probe_cache_entry__get_event(struct probe_cache_entry *entry,if (ret > probe_conf.max_probes)return -E2BIG ; - *tevs =zalloc (ret *sizeof (*tev)); + *tevs =calloc (ret,sizeof (*tev));if (!*tevs)return -ENOMEM ;
tools/perf/util/probe-finder.c +4 -4
@@ -1305,7 +1305,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) tev->point.offset);/* Expand special probe argument if exist */ - args =zalloc (sizeof (struct perf_probe_arg ) *MAX_PROBE_ARGS ); + args =calloc (MAX_PROBE_ARGS ,sizeof (struct perf_probe_arg ));if (args ==NULL ) { ret = -ENOMEM ; goto end;
@@ -1316,7 +1316,7 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) goto end; tev->nargs = ret; - tev->args =zalloc (sizeof (struct probe_trace_arg ) * tev->nargs); + tev->args =calloc (tev->nargs,sizeof (struct probe_trace_arg ));if (tev->args ==NULL ) { ret = -ENOMEM ; goto end;
@@ -1393,7 +1393,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,int ret, i;/* Allocate result tevs array */ - *tevs =zalloc (sizeof (struct probe_trace_event ) * tf.max_tevs); + *tevs =calloc (tf.max_tevs,sizeof (struct probe_trace_event ));if (*tevs ==NULL )return -ENOMEM ;
@@ -1566,7 +1566,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,int ret;/* Allocate result vls array */ - *vls =zalloc (sizeof (struct variable_list ) * af.max_vls); + *vls =calloc (af.max_vls,sizeof (struct variable_list ));if (*vls ==NULL )return -ENOMEM ;
tools/perf/util/session.c +1 -1
@@ -2533,7 +2533,7 @@ static int __perf_session__process_dir_events(struct perf_session *session) nr_readers++; } - rd =zalloc (nr_readers *sizeof (struct reader )); + rd =calloc (nr_readers,sizeof (struct reader ));if (!rd)return -ENOMEM ;
tools/perf/util/srcline.c +1 -0
@@ -12,6 +12,7 @@#include <inttypes.h> #include <string.h> #include <linux/string.h> +#include <linux/zalloc.h> bool srcline_full_filename;
tools/perf/util/stat-shadow.c +0 -1
@@ -13,7 +13,6 @@#include "metricgroup.h" #include "cgroup.h" #include "units.h" -#include <linux/zalloc.h> #include "iostat.h" #include "util/hashmap.h" #include "tool_pmu.h"
tools/perf/util/unwind-libunwind-local.c +0 -1
@@ -25,7 +25,6 @@#include <unistd.h> #include <sys/mman.h> #include <linux/list.h> -#include <linux/zalloc.h> #ifndef REMOTE_UNWIND_LIBUNWIND #include <libunwind.h> #include <libunwind-ptrace.h>
tools/perf/util/values.c +4 -4
@@ -13,9 +13,9 @@int perf_read_values_init (struct perf_read_values *values) { values->threads_max =16 ; - values->pid =malloc (values->threads_max *sizeof (*values->pid)); - values->tid =malloc (values->threads_max *sizeof (*values->tid)); - values->value =zalloc (values->threads_max *sizeof (*values->value)); + values->pid =calloc (values->threads_max,sizeof (*values->pid)); + values->tid =calloc (values->threads_max,sizeof (*values->tid)); + values->value =calloc (values->threads_max,sizeof (*values->value));if (!values->pid || !values->tid || !values->value) {pr_debug ("failed to allocate read_values threads arrays" ); goto out_free_pid;
@@ -96,7 +96,7 @@ static int perf_read_values__findnew_thread(struct perf_read_values *values, i = values->threads; - values->value[i] =zalloc (values->counters_max *sizeof (**values->value)); + values->value[i] =calloc (values->counters_max,sizeof (**values->value));if (!values->value[i]) {pr_debug ("failed to allocate read_values counters array" );return -ENOMEM ;
Signature · 1 line
2.53.0
Arnaldo Carvalho de Melo
maintainerPATCH v3 4/4 perf symbol: Add missing libgen.h include to get basename() prototype
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
This works in some places because there are tricks to prefer a GNU version instead of the XPG version, but started breaking in some musl libc systems due to some unrelated change that made libgen.h stop being included by chance.
Since symbol.h is using basename(), make it include libgen.h.
--- tools/perf/util/symbol.h | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/symbol.h +1 -0
@@ -10,6 +10,7 @@#include <linux/rbtree.h> #include <stdio.h> #include <errno.h> +#include <libgen.h> #include "addr_location.h" #include "path.h" #include "symbol_conf.h"
Signature · 1 line
2.53.0
Ian Rogers
reviewerRe: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
Quoted 9 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
This works in some places because there are tricks to prefer a GNU version instead of the XPG version, but started breaking in some musl libc systems due to some unrelated change that made
libgen.hstop being included by chance.Since
symbol.his usingbasename(), make it includelibgen.h.
Huh, this is being flagged by Sashiko:
"""
Could this include cause segmentation faults by exposing the POSIX version of basename() to the rest of the tools?
According to the POSIX specification, if the input is an empty string or slashes, basename() can return a pointer to a statically allocated, read-only string literal (such as "." or "/").
In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes the return value of basename() and modifies it in-place:
ptr = basename(exec);
...
for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) {
if (!isalnum(*ptr2) && *ptr2 != '_')
*ptr2 = '_';
}If a user provides an empty target or "/", the POSIX basename() would return a read-only string. When the loop attempts to write a null or underscore into it, wouldn't it trigger an immediate segmentation fault?
Is there a way we can safely duplicate the returned string before modifying it, or ensure the inputs never resolve to these static strings?
"""
The story of the two basename versions and the issues appears in the man pages. The whole thing makes me think we should avoid basename :-)
Thanks,
Ian
Quoted 20 lines
Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/util/symbol.h | 1 + 1 file changed, 1 insertion(+)tools/perf/util/symbol.h +1
-1@@ -10,6 +10,7 @@#include <linux/rbtree.h> #include <stdio.h> #include <errno.h> +#include <libgen.h> #include "addr_location.h" #include "path.h" #include "symbol_conf.h" --2.53.0
Re: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
Quoted 32 lines · Ian Rogers
Quoted 9 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
This works in some places because there are tricks to prefer a GNU version instead of the XPG version, but started breaking in some musl libc systems due to some unrelated change that made
libgen.hstop being included by chance.Since
symbol.his usingbasename(), make it includelibgen.h.Huh, this is being flagged by Sashiko:
"""
Could this include cause segmentation faults by exposing the POSIX version ofbasename()to the rest of the tools?According to the POSIX specification, if the input is an empty string or slashes,
basename()can return a pointer to a statically allocated, read-only string literal (such as "." or "/").In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes the return value of
basename()and modifies it in-place:ptr = basename(exec); ... for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) { if (!isalnum(*ptr2) && *ptr2 != '_') *ptr2 = '_'; }If a user provides an empty target or "/", the POSIX
basename()would return a read-only string. When the loop attempts to write a null or underscore into it, wouldn't it trigger an immediate segmentation fault?
Quoted 6 lines
Is there a way we can safely duplicate the returned string before modifying it, or ensure the inputs never resolve to these static strings?
"""The story of the two basename versions and the issues appears in the man pages. The whole thing makes me think we should avoid basename :-)
:-)
I think we should just follow it's advice and use strdup in it's return, in a helper, and have it's users free it afterwards.
- Arnaldo
Quoted 9 lines
Thanks,
IanQuoted 5 lines
Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/util/symbol.h | 1 + 1 file changed, 1 insertion(+)
Quoted 15 lines
Quoted 13 lines
tools/perf/util/symbol.h +1
-1@@ -10,6 +10,7 @@#include <linux/rbtree.h> #include <stdio.h> #include <errno.h> +#include <libgen.h> #include "addr_location.h" #include "path.h" #include "symbol_conf.h" --Quoted 2 lines
2.53.0
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
In reply to Ian RogersQuoted 32 lines · Ian Rogers
Quoted 9 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
This works in some places because there are tricks to prefer a GNU version instead of the XPG version, but started breaking in some musl libc systems due to some unrelated change that made
libgen.hstop being included by chance.Since
symbol.his usingbasename(), make it includelibgen.h.Huh, this is being flagged by Sashiko:
"""
Could this include cause segmentation faults by exposing the POSIX version ofbasename()to the rest of the tools?According to the POSIX specification, if the input is an empty string or slashes,
basename()can return a pointer to a statically allocated, read-only string literal (such as "." or "/").In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes the return value of
basename()and modifies it in-place:ptr = basename(exec); ... for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) { if (!isalnum(*ptr2) && *ptr2 != '_') *ptr2 = '_'; }If a user provides an empty target or "/", the POSIX
basename()would return a read-only string. When the loop attempts to write a null or underscore into it, wouldn't it trigger an immediate segmentation fault?
Quoted 6 lines
Is there a way we can safely duplicate the returned string before modifying it, or ensure the inputs never resolve to these static strings?
"""The story of the two basename versions and the issues appears in the man pages. The whole thing makes me think we should avoid basename :-)
What sashiko pointed out is a problem in some other part of perf that is unrelated to this patch and requires a separate fix, no?
I.e. tools/perf/util/probe-event.c already includes libgen.h since 581037151910126a ("perf probe: Add missing libgen.h header needed for using basename()") from March, 2024.
In the scope of this patch the potential problem isn't possible as __symbol__join_symfs() isn't changing the return of basename(), right?
⬢ [acme@toolbx perf-tools-next]$ git grep '\<basename(' tools/perftools/perf/builtin-daemon.c: base = basename(basen);tools/perf/scripts/python/exported-sql-viewer.py: if short_name[0:7] == "[kernel" and os.path.basename(long_name) == "kcore":
tools/perf/util/annotate.c: d_filename = basename(filename); tools/perf/util/dsos.c: * basename() may modify path buffer, so we must pass tools/perf/util/dsos.c: * basename() may return a pointer to internal tools/perf/util/dsos.c: base = strdup(basename(lname)); tools/perf/util/probe-event.c: ptr1 = basename(exec_copy); tools/perf/util/symbol.h: base = basename(path_copy);
⬢ [acme@toolbx perf-tools-next]$
It seems just the probe-event.c case needs fixing.
And we also have this in tools/perf/util/srcline.c:
/* basename version that takes a const input string */ static const char *gnu_basename(const char *path)
{
const char *base = strrchr(path, '/');
return base ? base + 1 : path;
}That probably could be reused to avoid these basename() oddities?
- Arnaldo
Ian Rogers
reviewerRe: [PATCH 4/4] perf symbol: Add missing libgen.h include to get basename() prototype
Quoted 38 lines · Arnaldo Carvalho de Melo
Quoted 36 lines · Ian Rogers
Quoted 9 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
This works in some places because there are tricks to prefer a GNU version instead of the XPG version, but started breaking in some musl libc systems due to some unrelated change that made
libgen.hstop being included by chance.Since
symbol.his usingbasename(), make it includelibgen.h.Huh, this is being flagged by Sashiko:
"""
Could this include cause segmentation faults by exposing the POSIX version ofbasename()to the rest of the tools?According to the POSIX specification, if the input is an empty string or slashes,
basename()can return a pointer to a statically allocated, read-only string literal (such as "." or "/").In tools/perf/util/probe-event.c:convert_exec_to_group(), the code takes the return value of
basename()and modifies it in-place:ptr = basename(exec); ... for (ptr2 = ptr; *ptr2 != '\0'; ptr2++) { if (!isalnum(*ptr2) && *ptr2 != '_') *ptr2 = '_'; }If a user provides an empty target or "/", the POSIX
basename()would return a read-only string. When the loop attempts to write a null or underscore into it, wouldn't it trigger an immediate segmentation fault?Is there a way we can safely duplicate the returned string before modifying it, or ensure the inputs never resolve to these static strings?
"""
Quoted 5 lines
Quoted 2 lines
The story of the two basename versions and the issues appears in the man pages. The whole thing makes me think we should avoid basename :-)
What sashiko pointed out is a problem in some other part of perf that is unrelated to this patch and requires a separate fix, no?
Not quite, from the man page:
With glibc, one gets the POSIX version of basename() when <libgen.h>
is included, and the GNU version otherwise.
As symbol.h is included in many places, then, with your change whenever symbol.h is used the behavior of basename will flip.
Quoted 24 lines
I.e.
tools/perf/util/probe-event.calready includeslibgen.hsince581037151910126a("perf probe: Add missinglibgen.hheader needed for usingbasename()") from March, 2024.In the scope of this patch the potential problem isn't possible as
__symbol__join_symfs()isn't changing the return ofbasename(), right?⬢ [acme@toolbx perf-tools-next]$ git grep '\<basename('
tools/perftools/perf/builtin-daemon.c: base = basename(basen);tools/perf/scripts/python/exported-sql-viewer.py: if short_name[0:7] == "[kernel" andos.path.basename(long_name)== "kcore":tools/perf/util/annotate.c: d_filename = basename(filename); tools/perf/util/dsos.c: * basename() may modify path buffer, so we must pass tools/perf/util/dsos.c: * basename() may return a pointer to internal tools/perf/util/dsos.c: base = strdup(basename(lname)); tools/perf/util/probe-event.c: ptr1 = basename(exec_copy); tools/perf/util/symbol.h: base = basename(path_copy);⬢ [acme@toolbx perf-tools-next]$
It seems just the
probe-event.ccase needs fixing.And we also have this in
tools/perf/util/srcline.c:/* basename version that takes a const input string */ static const char *gnu_basename(const char *path)
Quoted 7 lines
{ const char *base = strrchr(path, '/'); return base ? base + 1 : path; }That probably could be reused to avoid these
basename()oddities?
Yeah, probably move gnu_basename to util.c/h and then make all callers use gnu_basename and not basename. This would mean that symbol.h using util.h, instead could we move __symbol__join_symfs to symbol.c to avoid all these includes and touching a file requiring everything being rebuilt? The function is large enough that including it in the header doesn't make much sense.
Thanks,
Ian
Quoted 1 line
- Arnaldo