perf build: Fix build for clang's -Wunreachable-code
Subsystems (4)
Ian Rogers
reviewerRFC PATCH 1/1 perf build: Fix build for clang's -Wunreachable-code
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
--- tools/lib/bpf/libbpf.c | 3 + tools/perf/bench/breakpoint.c | 4 +- tools/perf/bench/epoll-ctl.c | 2 +- tools/perf/bench/epoll-wait.c | 2 +- tools/perf/bench/evlist-open-close.c | 2 +- tools/perf/bench/find-bit-bench.c | 2 +- tools/perf/bench/futex-hash.c | 2 +- tools/perf/bench/futex-lock-pi.c | 2 +- tools/perf/bench/futex-requeue.c | 2 +- tools/perf/bench/futex-wake-parallel.c | 2 +- tools/perf/bench/futex-wake.c | 2 +- tools/perf/bench/inject-buildid.c | 2 +- tools/perf/bench/kallsyms-parse.c | 2 +- tools/perf/bench/pmu-scan.c | 2 +- tools/perf/bench/synthesize.c | 2 +- tools/perf/builtin-check.c | 6 +- tools/perf/builtin-kvm.c | 5 +- tools/perf/builtin-kwork.c | 5 +- tools/perf/builtin-sched.c | 5 +- tools/perf/builtin-trace.c | 108 +++++++++--------- tools/perf/tests/perf-record.c | 10 +- tools/perf/util/Build | 1 + tools/perf/util/auxtrace.c | 2 +- .../scripting-engines/trace-event-python.c | 2 +- 24 files changed, 83 insertions(+), 94 deletions(-)
tools/lib/bpf/libbpf.c +3 -0
@@ -6823,6 +6823,8 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_proreturn true;break ;case BPF_PROG_TYPE_PERF_EVENT : +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&btf_is_struct (t) &&strcmp (tname,"pt_regs" ) ==0 )return true;
@@ -6832,6 +6834,7 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_pro if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&btf_is_struct (t) &&strcmp (tname,"user_regs_struct" ) ==0 )return true; +#pragma clang diagnostic popbreak ;case BPF_PROG_TYPE_RAW_TRACEPOINT :case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE :
tools/perf/bench/breakpoint.c +2 -2
@@ -119,7 +119,7 @@ int bench_breakpoint_thread(int argc, const char **argv)if (parse_options (argc, argv, thread_options, thread_usage,0 )) {usage_with_options (thread_usage, thread_options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } breakpoints =calloc (thread_params.nbreakpoints,sizeof (breakpoints[0 ])); parallel =calloc (thread_params.nparallel,sizeof (parallel[0 ]));
@@ -205,7 +205,7 @@ int bench_breakpoint_enable(int argc, const char **argv)if (parse_options (argc, argv, enable_options, enable_usage,0 )) {usage_with_options (enable_usage, enable_options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } fd =breakpoint_setup (&watched);
tools/perf/bench/epoll-ctl.c +1 -1
@@ -322,7 +322,7 @@ int bench_epoll_ctl(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_epoll_ctl_usage,0 );if (argc) {usage_with_options (bench_epoll_ctl_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));
tools/perf/bench/epoll-wait.c +1 -1
@@ -441,7 +441,7 @@ int bench_epoll_wait(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_epoll_wait_usage,0 );if (argc) {usage_with_options (bench_epoll_wait_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));
tools/perf/bench/evlist-open-close.c +1 -1
@@ -231,7 +231,7 @@ int bench_evlist_open_close(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } err =target__validate (&opts.target);
tools/perf/bench/find-bit-bench.c +1 -1
@@ -129,7 +129,7 @@ int bench_mem_find_bit(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }for (i =1 ; i <=2048 ; i <<=1 )
tools/perf/bench/futex-hash.c +1 -1
@@ -135,7 +135,7 @@ int bench_futex_hash(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_futex_hash_usage,0 );if (argc) {usage_with_options (bench_futex_hash_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } cpu =perf_cpu_map__new_online_cpus ();
tools/perf/bench/futex-lock-pi.c +1 -1
@@ -249,5 +249,5 @@ int bench_futex_lock_pi(int argc, const char **argv)return ret; err:usage_with_options (bench_futex_lock_pi_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }
tools/perf/bench/futex-requeue.c +1 -1
@@ -310,5 +310,5 @@ int bench_futex_requeue(int argc, const char **argv)return ret; err:usage_with_options (bench_futex_requeue_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }
tools/perf/bench/futex-wake-parallel.c +1 -1
@@ -251,7 +251,7 @@ int bench_futex_wake_parallel(int argc, const char **argv) bench_futex_wake_parallel_usage, 0);if (argc) {usage_with_options (bench_futex_wake_parallel_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));
tools/perf/bench/futex-wake.c +1 -1
@@ -146,7 +146,7 @@ int bench_futex_wake(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_futex_wake_usage,0 );if (argc) {usage_with_options (bench_futex_wake_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } cpu =perf_cpu_map__new_online_cpus ();
tools/perf/bench/inject-buildid.c +1 -1
@@ -478,7 +478,7 @@ int bench_inject_build_id(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }return do_inject_loops (&data);
tools/perf/bench/kallsyms-parse.c +1 -1
@@ -68,7 +68,7 @@ int bench_kallsyms_parse(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }return do_kallsyms_parse ();
tools/perf/bench/pmu-scan.c +1 -1
@@ -177,7 +177,7 @@ int bench_pmu_scan(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } err =run_pmu_scan ();
tools/perf/bench/synthesize.c +1 -1
@@ -242,7 +242,7 @@ int bench_synthesize(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }/*
tools/perf/builtin-check.c +1 -5
@@ -170,9 +170,5 @@ int cmd_check(int argc, const char **argv)/* If no subcommand matched above, print usage help */ pr_err ("Unknown subcommand: %s\n" , argv[0 ]);usage_with_options (check_usage, check_options); - -/* free usage string allocated by parse_options_subcommand */ -free ((void *)check_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }
tools/perf/builtin-kvm.c +1 -4
@@ -2126,8 +2126,5 @@ int cmd_kvm(int argc, const char **argv)else usage_with_options (kvm_usage, kvm_options); -/* free usage string allocated by parse_options_subcommand */ -free ((void *)kvm_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }
tools/perf/builtin-kwork.c +1 -4
@@ -2522,8 +2522,5 @@ int cmd_kwork(int argc, const char **argv) }else usage_with_options (kwork_usage, kwork_options); -/* free usage string allocated by parse_options_subcommand */ -free ((void *)kwork_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }
tools/perf/builtin-sched.c +1 -4
@@ -3965,8 +3965,5 @@ int cmd_sched(int argc, const char **argv)usage_with_options (sched_usage, sched_options); } -/* free usage string allocated by parse_options_subcommand */ -free ((void *)sched_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }
tools/perf/builtin-trace.c +53 -55
166 lines not on this page · raw message
tools/perf/tests/perf-record.c +5 -5
@@ -67,7 +67,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest found_ld_mmap = false;int err =-1 , errs =0 , i, wakeups =0 ;u32 cpu; -int total_events =0 , nr_events[PERF_RECORD_MAX ] = {0 , }; +int /*total_events = 0, */ nr_events[PERF_RECORD_MAX ] = {0 , };char sbuf[STRERR_BUFSIZE ];perf_sample__init (&sample,/*all=*/ false);
@@ -165,7 +165,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestevlist__start_workload (evlist);while (1 ) { -int before = total_events; +// int before = total_events; for (i =0 ; i < evlist->core.nr_mmaps; i++) {union perf_event *event;
@@ -179,7 +179,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestconst u32 type = event->header.type;const char *name =perf_event__name (type); - ++total_events; +//++total_events; if (type <PERF_RECORD_MAX ) nr_events[type]++;
@@ -288,8 +288,8 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest* PERF_RECORD_{!SAMPLE} events don't honour * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does. */ -if (total_events == before && false) -evlist__poll (evlist,-1 ); +// if (total_events == before && false) +// evlist__poll(evlist, -1); sleep (1 );if (++wakeups >5 ) {
tools/perf/util/Build +1 -0
@@ -170,6 +170,7 @@ perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_ftrace.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_off_cpu.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-flex.o +CFLAGS_bpf-filter-flex.o += -Wno-unreachable-code perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-bison.o perf-util-$(CONFIG_PERF_BPF_SKEL) += btf.o
tools/perf/util/auxtrace.c +1 -1
@@ -384,7 +384,7 @@ static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,if (!buffer->data) goto out_free; buffer->data_needs_freeing = true; - }else if (BITS_PER_LONG ==32 && + }else if (BITS_PER_LONG == (32 ) && buffer->size >BUFFER_LIMIT_FOR_32_BIT ) { err =auxtrace_queues__split_buffer (queues, idx, buffer);if (err)
tools/perf/util/scripting-engines/trace-event-python.c +1 -1
@@ -116,7 +116,7 @@ static void handler_call_die(const char *handler_name)Py_FatalError ("problem in Python trace event handler" );// Py_FatalError does not return // but we have to make the compiler happy -abort (); +__builtin_unreachable (); }/*
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
Namhyung Kim
maintainerHi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Thanks,
Namhyung
Quoted 554 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/lib/bpf/libbpf.c | 3 + tools/perf/bench/breakpoint.c | 4 +- tools/perf/bench/epoll-ctl.c | 2 +- tools/perf/bench/epoll-wait.c | 2 +- tools/perf/bench/evlist-open-close.c | 2 +- tools/perf/bench/find-bit-bench.c | 2 +- tools/perf/bench/futex-hash.c | 2 +- tools/perf/bench/futex-lock-pi.c | 2 +- tools/perf/bench/futex-requeue.c | 2 +- tools/perf/bench/futex-wake-parallel.c | 2 +- tools/perf/bench/futex-wake.c | 2 +- tools/perf/bench/inject-buildid.c | 2 +- tools/perf/bench/kallsyms-parse.c | 2 +- tools/perf/bench/pmu-scan.c | 2 +- tools/perf/bench/synthesize.c | 2 +- tools/perf/builtin-check.c | 6 +- tools/perf/builtin-kvm.c | 5 +- tools/perf/builtin-kwork.c | 5 +- tools/perf/builtin-sched.c | 5 +- tools/perf/builtin-trace.c | 108 +++++++++--------- tools/perf/tests/perf-record.c | 10 +- tools/perf/util/Build | 1 + tools/perf/util/auxtrace.c | 2 +- .../scripting-engines/trace-event-python.c | 2 +- 24 files changed, 83 insertions(+), 94 deletions(-)tools/lib/bpf/libbpf.c +3
-0@@ -6823,6 +6823,8 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_proreturn true;break ;case BPF_PROG_TYPE_PERF_EVENT : +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&btf_is_struct (t) &&strcmp (tname,"pt_regs" ) ==0 )return true;@@ -6832,6 +6834,7 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_pro if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&btf_is_struct (t) &&strcmp (tname,"user_regs_struct" ) ==0 )return true; +#pragma clang diagnostic popbreak ;case BPF_PROG_TYPE_RAW_TRACEPOINT :case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE :tools/perf/bench/breakpoint.c +2
-2@@ -119,7 +119,7 @@ int bench_breakpoint_thread(int argc, const char **argv)if (parse_options (argc, argv, thread_options, thread_usage,0 )) {usage_with_options (thread_usage, thread_options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } breakpoints =calloc (thread_params.nbreakpoints,sizeof (breakpoints[0 ])); parallel =calloc (thread_params.nparallel,sizeof (parallel[0 ]));@@ -205,7 +205,7 @@ int bench_breakpoint_enable(int argc, const char **argv)if (parse_options (argc, argv, enable_options, enable_usage,0 )) {usage_with_options (enable_usage, enable_options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } fd =breakpoint_setup (&watched);tools/perf/bench/epoll-ctl.c +1
-1@@ -322,7 +322,7 @@ int bench_epoll_ctl(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_epoll_ctl_usage,0 );if (argc) {usage_with_options (bench_epoll_ctl_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));tools/perf/bench/epoll-wait.c +1
-1@@ -441,7 +441,7 @@ int bench_epoll_wait(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_epoll_wait_usage,0 );if (argc) {usage_with_options (bench_epoll_wait_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));tools/perf/bench/evlist-open-close.c +1
-1@@ -231,7 +231,7 @@ int bench_evlist_open_close(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } err =target__validate (&opts.target);tools/perf/bench/find-bit-bench.c +1
-1@@ -129,7 +129,7 @@ int bench_mem_find_bit(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }for (i =1 ; i <=2048 ; i <<=1 )tools/perf/bench/futex-hash.c +1
-1@@ -135,7 +135,7 @@ int bench_futex_hash(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_futex_hash_usage,0 );if (argc) {usage_with_options (bench_futex_hash_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } cpu =perf_cpu_map__new_online_cpus ();tools/perf/bench/futex-lock-pi.c +1
-1@@ -249,5 +249,5 @@ int bench_futex_lock_pi(int argc, const char **argv)return ret; err:usage_with_options (bench_futex_lock_pi_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }tools/perf/bench/futex-requeue.c +1
-1@@ -310,5 +310,5 @@ int bench_futex_requeue(int argc, const char **argv)return ret; err:usage_with_options (bench_futex_requeue_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }tools/perf/bench/futex-wake-parallel.c +1
-1@@ -251,7 +251,7 @@ int bench_futex_wake_parallel(int argc, const char **argv) bench_futex_wake_parallel_usage, 0);if (argc) {usage_with_options (bench_futex_wake_parallel_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));tools/perf/bench/futex-wake.c +1
-1@@ -146,7 +146,7 @@ int bench_futex_wake(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_futex_wake_usage,0 );if (argc) {usage_with_options (bench_futex_wake_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } cpu =perf_cpu_map__new_online_cpus ();tools/perf/bench/inject-buildid.c +1
-1@@ -478,7 +478,7 @@ int bench_inject_build_id(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }return do_inject_loops (&data);tools/perf/bench/kallsyms-parse.c +1
-1@@ -68,7 +68,7 @@ int bench_kallsyms_parse(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }return do_kallsyms_parse ();tools/perf/bench/pmu-scan.c +1
-1@@ -177,7 +177,7 @@ int bench_pmu_scan(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } err =run_pmu_scan ();tools/perf/bench/synthesize.c +1
-1@@ -242,7 +242,7 @@ int bench_synthesize(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }/* tools/perf/builtin-check.c +1
-5@@ -170,9 +170,5 @@ int cmd_check(int argc, const char **argv)/* If no subcommand matched above, print usage help */ pr_err ("Unknown subcommand: %s\n" , argv[0 ]);usage_with_options (check_usage, check_options); - -/* free usage string allocated by parse_options_subcommand */ -free ((void *)check_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-kvm.c +1
-4@@ -2126,8 +2126,5 @@ int cmd_kvm(int argc, const char **argv)else usage_with_options (kvm_usage, kvm_options); -/* free usage string allocated by parse_options_subcommand */ -free ((void *)kvm_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-kwork.c +1
-4@@ -2522,8 +2522,5 @@ int cmd_kwork(int argc, const char **argv) }else usage_with_options (kwork_usage, kwork_options); -/* free usage string allocated by parse_options_subcommand */ -free ((void *)kwork_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-sched.c +1
-4@@ -3965,8 +3965,5 @@ int cmd_sched(int argc, const char **argv)usage_with_options (sched_usage, sched_options); } -/* free usage string allocated by parse_options_subcommand */ -free ((void *)sched_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-trace.c +53
-55@@ -4378,25 +4378,34 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->live = true;if (!trace->raw_augmented_syscalls) { -if (trace->trace_syscalls &&trace__add_syscall_newtp (trace)) - goto out_error_raw_syscalls; +if (trace->trace_syscalls &&trace__add_syscall_newtp (trace)) { +char errbuf[BUFSIZ ]; +tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf), +"raw_syscalls" ,"sys_(enter|exit)" ); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }if (trace->trace_syscalls) trace->vfs_getname =evlist__add_vfs_getname (evlist); }if ((trace->trace_pgfaults &TRACE_PFMAJ )) { pgfault_maj =evsel__new_pgfault (PERF_COUNT_SW_PAGE_FAULTS_MAJ ); -if (pgfault_maj ==NULL ) - goto out_error_mem; +if (pgfault_maj ==NULL ) { +fprintf (trace->output,"Not enough memory to run!\n" ); + goto out_delete_evlist; + }evsel__config_callchain (pgfault_maj, &trace->opts, &callchain_param);evlist__add (evlist, pgfault_maj); }if ((trace->trace_pgfaults &TRACE_PFMIN )) { pgfault_min =evsel__new_pgfault (PERF_COUNT_SW_PAGE_FAULTS_MIN ); -if (pgfault_min ==NULL ) - goto out_error_mem; +if (pgfault_min ==NULL ) { +fprintf (trace->output,"Not enough memory to run!\n" ); + goto out_delete_evlist; + }evsel__config_callchain (pgfault_min, &trace->opts, &callchain_param);evlist__add (evlist, pgfault_min); }@@ -4405,8 +4414,14 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->opts.ignore_missing_thread = trace->opts.target.uid !=UINT_MAX || trace->opts.target.pid;if (trace->sched && -evlist__add_newtp (evlist,"sched" ,"sched_stat_runtime" , trace__sched_stat_runtime)) - goto out_error_sched_stat_runtime; +evlist__add_newtp (evlist,"sched" ,"sched_stat_runtime" , trace__sched_stat_runtime)) { +char errbuf[BUFSIZ ]; + +tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf), +"sched" ,"sched_stat_runtime" ); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }/* * If a global cgroup was set, apply it to all the events without an * explicit cgroup. I.e.: @@ -4465,8 +4480,13 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } err =evlist__open (evlist); -if (err <0 ) - goto out_error_open; +if (err <0 ) { +char errbuf[BUFSIZ ]; + +evlist__strerror_open (evlist, errno, errbuf,sizeof (errbuf)); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }#ifdef HAVE_BPF_SKEL if (trace->syscalls.events.bpf_output) {struct perf_cpu cpu;@@ -4490,8 +4510,10 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->filter_pids.map = trace->skel->maps.pids_filtered;#endif err =trace__set_filter_pids (trace); -if (err <0 ) - goto out_error_mem; +if (err <0 ) { +fprintf (trace->output,"Not enough memory to run!\n" ); + goto out_delete_evlist; + }#ifdef HAVE_BPF_SKEL if (trace->skel && trace->skel->progs.sys_enter) {@@ -4505,9 +4527,10 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (trace->ev_qualifier_ids.nr >0 ) { err =trace__set_ev_qualifier_filter (trace); -if (err <0 ) - goto out_errno; - +if (err <0 ) { +fprintf (trace->output,"errno=%d,%s\n" , errno,strerror (errno)); + goto out_delete_evlist; + }if (trace->syscalls.events.sys_exit) {pr_debug ("event qualifier tracepoint filter: %s\n" , trace->syscalls.events.sys_exit->filter);@@ -4532,13 +4555,24 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (err) goto out_delete_evlist; err =evlist__apply_filters (evlist, &evsel, &trace->opts.target); -if (err <0 ) - goto out_error_apply_filters; +if (err <0 ) { +char errbuf[BUFSIZ ]; + +fprintf (trace->output, +"Failed to set filter \"%s\" on event %s with %d (%s)\n" , + evsel->filter,evsel__name (evsel), errno, +str_error_r (errno, errbuf,sizeof (errbuf))); + goto out_delete_evlist; + } err =evlist__mmap (evlist, trace->opts.mmap_pages); -if (err <0 ) - goto out_error_mmap; +if (err <0 ) { +char errbuf[BUFSIZ ]; +evlist__strerror_mmap (evlist, errno, errbuf,sizeof (errbuf)); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist);@@ -4646,42 +4680,6 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->evlist =NULL ; trace->live = false;return err; -{ -char errbuf[BUFSIZ ]; - -out_error_sched_stat_runtime: -tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf),"sched" ,"sched_stat_runtime" ); - goto out_error; - -out_error_raw_syscalls: -tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf),"raw_syscalls" ,"sys_(enter|exit)" ); - goto out_error; - -out_error_mmap: -evlist__strerror_mmap (evlist, errno, errbuf,sizeof (errbuf)); - goto out_error; - -out_error_open: -evlist__strerror_open (evlist, errno, errbuf,sizeof (errbuf)); - -out_error: -fprintf (trace->output,"%s\n" , errbuf); - goto out_delete_evlist; - -out_error_apply_filters: -fprintf (trace->output, -"Failed to set filter \"%s\" on event %s with %d (%s)\n" , - evsel->filter,evsel__name (evsel), errno, -str_error_r (errno, errbuf,sizeof (errbuf))); - goto out_delete_evlist; -} -out_error_mem: -fprintf (trace->output,"Not enough memory to run!\n" ); - goto out_delete_evlist; - -out_errno: -fprintf (trace->output,"errno=%d,%s\n" , errno,strerror (errno)); - goto out_delete_evlist; }static int trace__replay (struct trace *trace)tools/perf/tests/perf-record.c +5
-5@@ -67,7 +67,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest found_ld_mmap = false;int err =-1 , errs =0 , i, wakeups =0 ;u32 cpu; -int total_events =0 , nr_events[PERF_RECORD_MAX ] = {0 , }; +int /*total_events = 0, */ nr_events[PERF_RECORD_MAX ] = {0 , };char sbuf[STRERR_BUFSIZE ];perf_sample__init (&sample,/*all=*/ false);@@ -165,7 +165,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestevlist__start_workload (evlist);while (1 ) { -int before = total_events; +// int before = total_events; for (i =0 ; i < evlist->core.nr_mmaps; i++) {union perf_event *event;@@ -179,7 +179,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestconst u32 type = event->header.type;const char *name =perf_event__name (type); - ++total_events; +//++total_events; if (type <PERF_RECORD_MAX ) nr_events[type]++;@@ -288,8 +288,8 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest* PERF_RECORD_{!SAMPLE} events don't honour * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does. */ -if (total_events == before && false) -evlist__poll (evlist,-1 ); +// if (total_events == before && false) +// evlist__poll(evlist, -1); sleep (1 );if (++wakeups >5 ) {tools/perf/util/Build +1
-0@@ -170,6 +170,7 @@ perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_ftrace.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_off_cpu.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-flex.o +CFLAGS_bpf-filter-flex.o += -Wno-unreachable-code perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-bison.o perf-util-$(CONFIG_PERF_BPF_SKEL) += btf.otools/perf/util/auxtrace.c +1
-1@@ -384,7 +384,7 @@ static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,if (!buffer->data) goto out_free; buffer->data_needs_freeing = true; - }else if (BITS_PER_LONG ==32 && + }else if (BITS_PER_LONG == (32 ) && buffer->size >BUFFER_LIMIT_FOR_32_BIT ) { err =auxtrace_queues__split_buffer (queues, idx, buffer);if (err)tools/perf/util/scripting-engines/trace-event-python.c +1
-1@@ -116,7 +116,7 @@ static void handler_call_die(const char *handler_name)Py_FatalError ("problem in Python trace event handler" );// Py_FatalError does not return // but we have to make the compiler happy -abort (); +__builtin_unreachable (); }/* Signature · 1 line
2.49.0.604.gff1f9ca942-goog
Ian Rogers
reviewerQuoted 11 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
Quoted 2 lines
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```
Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Thanks,
Ian
Quoted 557 lines
Thanks,
NamhyungQuoted 554 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/lib/bpf/libbpf.c | 3 + tools/perf/bench/breakpoint.c | 4 +- tools/perf/bench/epoll-ctl.c | 2 +- tools/perf/bench/epoll-wait.c | 2 +- tools/perf/bench/evlist-open-close.c | 2 +- tools/perf/bench/find-bit-bench.c | 2 +- tools/perf/bench/futex-hash.c | 2 +- tools/perf/bench/futex-lock-pi.c | 2 +- tools/perf/bench/futex-requeue.c | 2 +- tools/perf/bench/futex-wake-parallel.c | 2 +- tools/perf/bench/futex-wake.c | 2 +- tools/perf/bench/inject-buildid.c | 2 +- tools/perf/bench/kallsyms-parse.c | 2 +- tools/perf/bench/pmu-scan.c | 2 +- tools/perf/bench/synthesize.c | 2 +- tools/perf/builtin-check.c | 6 +- tools/perf/builtin-kvm.c | 5 +- tools/perf/builtin-kwork.c | 5 +- tools/perf/builtin-sched.c | 5 +- tools/perf/builtin-trace.c | 108 +++++++++--------- tools/perf/tests/perf-record.c | 10 +- tools/perf/util/Build | 1 + tools/perf/util/auxtrace.c | 2 +- .../scripting-engines/trace-event-python.c | 2 +- 24 files changed, 83 insertions(+), 94 deletions(-)tools/lib/bpf/libbpf.c +3
-0@@ -6823,6 +6823,8 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_proreturn true;break ;case BPF_PROG_TYPE_PERF_EVENT : +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&btf_is_struct (t) &&strcmp (tname,"pt_regs" ) ==0 )return true;@@ -6832,6 +6834,7 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_pro if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&btf_is_struct (t) &&strcmp (tname,"user_regs_struct" ) ==0 )return true; +#pragma clang diagnostic popbreak ;case BPF_PROG_TYPE_RAW_TRACEPOINT :case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE :tools/perf/bench/breakpoint.c +2
-2@@ -119,7 +119,7 @@ int bench_breakpoint_thread(int argc, const char **argv)if (parse_options (argc, argv, thread_options, thread_usage,0 )) {usage_with_options (thread_usage, thread_options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } breakpoints =calloc (thread_params.nbreakpoints,sizeof (breakpoints[0 ])); parallel =calloc (thread_params.nparallel,sizeof (parallel[0 ]));@@ -205,7 +205,7 @@ int bench_breakpoint_enable(int argc, const char **argv)if (parse_options (argc, argv, enable_options, enable_usage,0 )) {usage_with_options (enable_usage, enable_options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } fd =breakpoint_setup (&watched);tools/perf/bench/epoll-ctl.c +1
-1@@ -322,7 +322,7 @@ int bench_epoll_ctl(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_epoll_ctl_usage,0 );if (argc) {usage_with_options (bench_epoll_ctl_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));tools/perf/bench/epoll-wait.c +1
-1@@ -441,7 +441,7 @@ int bench_epoll_wait(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_epoll_wait_usage,0 );if (argc) {usage_with_options (bench_epoll_wait_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));tools/perf/bench/evlist-open-close.c +1
-1@@ -231,7 +231,7 @@ int bench_evlist_open_close(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } err =target__validate (&opts.target);tools/perf/bench/find-bit-bench.c +1
-1@@ -129,7 +129,7 @@ int bench_mem_find_bit(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }for (i =1 ; i <=2048 ; i <<=1 )tools/perf/bench/futex-hash.c +1
-1@@ -135,7 +135,7 @@ int bench_futex_hash(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_futex_hash_usage,0 );if (argc) {usage_with_options (bench_futex_hash_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } cpu =perf_cpu_map__new_online_cpus ();tools/perf/bench/futex-lock-pi.c +1
-1@@ -249,5 +249,5 @@ int bench_futex_lock_pi(int argc, const char **argv)return ret; err:usage_with_options (bench_futex_lock_pi_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }tools/perf/bench/futex-requeue.c +1
-1@@ -310,5 +310,5 @@ int bench_futex_requeue(int argc, const char **argv)return ret; err:usage_with_options (bench_futex_requeue_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }tools/perf/bench/futex-wake-parallel.c +1
-1@@ -251,7 +251,7 @@ int bench_futex_wake_parallel(int argc, const char **argv) bench_futex_wake_parallel_usage, 0);if (argc) {usage_with_options (bench_futex_wake_parallel_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }memset (&act,0 ,sizeof (act));tools/perf/bench/futex-wake.c +1
-1@@ -146,7 +146,7 @@ int bench_futex_wake(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_futex_wake_usage,0 );if (argc) {usage_with_options (bench_futex_wake_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } cpu =perf_cpu_map__new_online_cpus ();tools/perf/bench/inject-buildid.c +1
-1@@ -478,7 +478,7 @@ int bench_inject_build_id(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }return do_inject_loops (&data);tools/perf/bench/kallsyms-parse.c +1
-1@@ -68,7 +68,7 @@ int bench_kallsyms_parse(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }return do_kallsyms_parse ();tools/perf/bench/pmu-scan.c +1
-1@@ -177,7 +177,7 @@ int bench_pmu_scan(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); } err =run_pmu_scan ();tools/perf/bench/synthesize.c +1
-1@@ -242,7 +242,7 @@ int bench_synthesize(int argc, const char **argv) argc =parse_options (argc, argv, options, bench_usage,0 );if (argc) {usage_with_options (bench_usage, options); -exit (EXIT_FAILURE ); +__builtin_unreachable (); }/* tools/perf/builtin-check.c +1
-5@@ -170,9 +170,5 @@ int cmd_check(int argc, const char **argv)/* If no subcommand matched above, print usage help */ pr_err ("Unknown subcommand: %s\n" , argv[0 ]);usage_with_options (check_usage, check_options); - -/* free usage string allocated by parse_options_subcommand */ -free ((void *)check_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-kvm.c +1
-4@@ -2126,8 +2126,5 @@ int cmd_kvm(int argc, const char **argv)else usage_with_options (kvm_usage, kvm_options); -/* free usage string allocated by parse_options_subcommand */ -free ((void *)kvm_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-kwork.c +1
-4@@ -2522,8 +2522,5 @@ int cmd_kwork(int argc, const char **argv) }else usage_with_options (kwork_usage, kwork_options); -/* free usage string allocated by parse_options_subcommand */ -free ((void *)kwork_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-sched.c +1
-4@@ -3965,8 +3965,5 @@ int cmd_sched(int argc, const char **argv)usage_with_options (sched_usage, sched_options); } -/* free usage string allocated by parse_options_subcommand */ -free ((void *)sched_usage[0 ]); - -return 0 ; +__builtin_unreachable (); }tools/perf/builtin-trace.c +53
-55@@ -4378,25 +4378,34 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->live = true;if (!trace->raw_augmented_syscalls) { -if (trace->trace_syscalls &&trace__add_syscall_newtp (trace)) - goto out_error_raw_syscalls; +if (trace->trace_syscalls &&trace__add_syscall_newtp (trace)) { +char errbuf[BUFSIZ ]; +tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf), +"raw_syscalls" ,"sys_(enter|exit)" ); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }if (trace->trace_syscalls) trace->vfs_getname =evlist__add_vfs_getname (evlist); }if ((trace->trace_pgfaults &TRACE_PFMAJ )) { pgfault_maj =evsel__new_pgfault (PERF_COUNT_SW_PAGE_FAULTS_MAJ ); -if (pgfault_maj ==NULL ) - goto out_error_mem; +if (pgfault_maj ==NULL ) { +fprintf (trace->output,"Not enough memory to run!\n" ); + goto out_delete_evlist; + }evsel__config_callchain (pgfault_maj, &trace->opts, &callchain_param);evlist__add (evlist, pgfault_maj); }if ((trace->trace_pgfaults &TRACE_PFMIN )) { pgfault_min =evsel__new_pgfault (PERF_COUNT_SW_PAGE_FAULTS_MIN ); -if (pgfault_min ==NULL ) - goto out_error_mem; +if (pgfault_min ==NULL ) { +fprintf (trace->output,"Not enough memory to run!\n" ); + goto out_delete_evlist; + }evsel__config_callchain (pgfault_min, &trace->opts, &callchain_param);evlist__add (evlist, pgfault_min); }@@ -4405,8 +4414,14 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->opts.ignore_missing_thread = trace->opts.target.uid !=UINT_MAX || trace->opts.target.pid;if (trace->sched && -evlist__add_newtp (evlist,"sched" ,"sched_stat_runtime" , trace__sched_stat_runtime)) - goto out_error_sched_stat_runtime; +evlist__add_newtp (evlist,"sched" ,"sched_stat_runtime" , trace__sched_stat_runtime)) { +char errbuf[BUFSIZ ]; + +tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf), +"sched" ,"sched_stat_runtime" ); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }/* * If a global cgroup was set, apply it to all the events without an * explicit cgroup. I.e.: @@ -4465,8 +4480,13 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } err =evlist__open (evlist); -if (err <0 ) - goto out_error_open; +if (err <0 ) { +char errbuf[BUFSIZ ]; + +evlist__strerror_open (evlist, errno, errbuf,sizeof (errbuf)); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }#ifdef HAVE_BPF_SKEL if (trace->syscalls.events.bpf_output) {struct perf_cpu cpu;@@ -4490,8 +4510,10 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->filter_pids.map = trace->skel->maps.pids_filtered;#endif err =trace__set_filter_pids (trace); -if (err <0 ) - goto out_error_mem; +if (err <0 ) { +fprintf (trace->output,"Not enough memory to run!\n" ); + goto out_delete_evlist; + }#ifdef HAVE_BPF_SKEL if (trace->skel && trace->skel->progs.sys_enter) {@@ -4505,9 +4527,10 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (trace->ev_qualifier_ids.nr >0 ) { err =trace__set_ev_qualifier_filter (trace); -if (err <0 ) - goto out_errno; - +if (err <0 ) { +fprintf (trace->output,"errno=%d,%s\n" , errno,strerror (errno)); + goto out_delete_evlist; + }if (trace->syscalls.events.sys_exit) {pr_debug ("event qualifier tracepoint filter: %s\n" , trace->syscalls.events.sys_exit->filter);@@ -4532,13 +4555,24 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (err) goto out_delete_evlist; err =evlist__apply_filters (evlist, &evsel, &trace->opts.target); -if (err <0 ) - goto out_error_apply_filters; +if (err <0 ) { +char errbuf[BUFSIZ ]; + +fprintf (trace->output, +"Failed to set filter \"%s\" on event %s with %d (%s)\n" , + evsel->filter,evsel__name (evsel), errno, +str_error_r (errno, errbuf,sizeof (errbuf))); + goto out_delete_evlist; + } err =evlist__mmap (evlist, trace->opts.mmap_pages); -if (err <0 ) - goto out_error_mmap; +if (err <0 ) { +char errbuf[BUFSIZ ]; +evlist__strerror_mmap (evlist, errno, errbuf,sizeof (errbuf)); +fprintf (trace->output,"%s\n" , errbuf); + goto out_delete_evlist; + }if (!target__none (&trace->opts.target) && !trace->opts.target.initial_delay)evlist__enable (evlist);@@ -4646,42 +4680,6 @@ static int trace__run(struct trace *trace, int argc, const char **argv) trace->evlist =NULL ; trace->live = false;return err; -{ -char errbuf[BUFSIZ ]; - -out_error_sched_stat_runtime: -tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf),"sched" ,"sched_stat_runtime" ); - goto out_error; - -out_error_raw_syscalls: -tracing_path__strerror_open_tp (errno, errbuf,sizeof (errbuf),"raw_syscalls" ,"sys_(enter|exit)" ); - goto out_error; - -out_error_mmap: -evlist__strerror_mmap (evlist, errno, errbuf,sizeof (errbuf)); - goto out_error; - -out_error_open: -evlist__strerror_open (evlist, errno, errbuf,sizeof (errbuf)); - -out_error: -fprintf (trace->output,"%s\n" , errbuf); - goto out_delete_evlist; - -out_error_apply_filters: -fprintf (trace->output, -"Failed to set filter \"%s\" on event %s with %d (%s)\n" , - evsel->filter,evsel__name (evsel), errno, -str_error_r (errno, errbuf,sizeof (errbuf))); - goto out_delete_evlist; -} -out_error_mem: -fprintf (trace->output,"Not enough memory to run!\n" ); - goto out_delete_evlist; - -out_errno: -fprintf (trace->output,"errno=%d,%s\n" , errno,strerror (errno)); - goto out_delete_evlist; }static int trace__replay (struct trace *trace)tools/perf/tests/perf-record.c +5
-5@@ -67,7 +67,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest found_ld_mmap = false;int err =-1 , errs =0 , i, wakeups =0 ;u32 cpu; -int total_events =0 , nr_events[PERF_RECORD_MAX ] = {0 , }; +int /*total_events = 0, */ nr_events[PERF_RECORD_MAX ] = {0 , };char sbuf[STRERR_BUFSIZE ];perf_sample__init (&sample,/*all=*/ false);@@ -165,7 +165,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestevlist__start_workload (evlist);while (1 ) { -int before = total_events; +// int before = total_events; for (i =0 ; i < evlist->core.nr_mmaps; i++) {union perf_event *event;@@ -179,7 +179,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestconst u32 type = event->header.type;const char *name =perf_event__name (type); - ++total_events; +//++total_events; if (type <PERF_RECORD_MAX ) nr_events[type]++;@@ -288,8 +288,8 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest* PERF_RECORD_{!SAMPLE} events don't honour * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does. */ -if (total_events == before && false) -evlist__poll (evlist,-1 ); +// if (total_events == before && false) +// evlist__poll(evlist, -1); sleep (1 );if (++wakeups >5 ) {tools/perf/util/Build +1
-0@@ -170,6 +170,7 @@ perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_ftrace.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_off_cpu.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-flex.o +CFLAGS_bpf-filter-flex.o += -Wno-unreachable-code perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-bison.o perf-util-$(CONFIG_PERF_BPF_SKEL) += btf.otools/perf/util/auxtrace.c +1
-1@@ -384,7 +384,7 @@ static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,if (!buffer->data) goto out_free; buffer->data_needs_freeing = true; - }else if (BITS_PER_LONG ==32 && + }else if (BITS_PER_LONG == (32 ) && buffer->size >BUFFER_LIMIT_FOR_32_BIT ) { err =auxtrace_queues__split_buffer (queues, idx, buffer);if (err)tools/perf/util/scripting-engines/trace-event-python.c +1
-2@@ -116,7 +116,7 @@ static void handler_call_die(const char *handler_name)Py_FatalError ("problem in Python trace event handler" );// Py_FatalError does not return // but we have to make the compiler happy -abort (); +__builtin_unreachable (); }/* -- 2.49.0.604.gff1f9ca942-goog
Ian Rogers
reviewerQuoted 22 lines · Ian Rogers
Quoted 11 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
Quoted 2 lines
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```
Quoted 5 lines
Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Ping. Thanks,
Ian
Namhyung Kim
maintainerQuoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace exit() to __builtin_unreachable() - seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 31 lines · Namhyung Kim
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Quoted 4 lines
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace
exit()to__builtin_unreachable()- seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?
Yep. The exit is incorrect (depending on your notion of correct, I'd go with clang's notion as they've had to consider this for a while) as it can never be executed. I've added the __builtin_unreachable() to document that you can never get to that statement, as otherwise it can make the code readability harder with the code looking like it will fall through after calling something like usage_with_options (which is noreturn). In unoptimized builds __builtin_unreachable() will fail if executed, so it is a bit more active than just a comment.
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 37 lines · Ian Rogers
Quoted 31 lines · Namhyung Kim
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Quoted 4 lines
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace
exit()to__builtin_unreachable()- seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?
Quoted 8 lines
Yep. The exit is incorrect (depending on your notion of correct, I'd go with clang's notion as they've had to consider this for a while) as it can never be executed. I've added the
__builtin_unreachable()to document that you can never get to that statement, as otherwise it can make the code readability harder with the code looking like it will fall through after calling something likeusage_with_options(which is noreturn). In unoptimized builds__builtin_unreachable()will fail if executed, so it is a bit more active than just a comment.
Oh I see, usage_with_options() calls exit() inside so any code after that won't be executed. Hmm.. isn't it better to remove those codes then?
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 48 lines · Namhyung Kim
Quoted 37 lines · Ian Rogers
Quoted 31 lines · Namhyung Kim
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Quoted 4 lines
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace
exit()to__builtin_unreachable()- seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?Quoted 8 lines
Yep. The exit is incorrect (depending on your notion of correct, I'd go with clang's notion as they've had to consider this for a while) as it can never be executed. I've added the
__builtin_unreachable()to document that you can never get to that statement, as otherwise it can make the code readability harder with the code looking like it will fall through after calling something likeusage_with_options(which is noreturn). In unoptimized builds__builtin_unreachable()will fail if executed, so it is a bit more active than just a comment.
Quoted 3 lines
Oh I see,
usage_with_options()callsexit()inside so any code after that won't be executed. Hmm.. isn't it better to remove those codes then?
Not sure I follow. The patch does remove the code but it replaces it with __builtin_unreachable() to basically state that the code here and below can never be reached. Do you mean remove the exit from usage_with_options? Then we'd need to fix all the callers, which would be a larger patch. Perhaps it should be usage_with_options_and_exit() to make it clearer that the code doesn't return. I was after doing what was minimal for -Wunreachable-code but while trying to keep the code clear.
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 53 lines · Ian Rogers
Quoted 48 lines · Namhyung Kim
Quoted 37 lines · Ian Rogers
Quoted 31 lines · Namhyung Kim
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Quoted 4 lines
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace
exit()to__builtin_unreachable()- seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?Quoted 8 lines
Yep. The exit is incorrect (depending on your notion of correct, I'd go with clang's notion as they've had to consider this for a while) as it can never be executed. I've added the
__builtin_unreachable()to document that you can never get to that statement, as otherwise it can make the code readability harder with the code looking like it will fall through after calling something likeusage_with_options(which is noreturn). In unoptimized builds__builtin_unreachable()will fail if executed, so it is a bit more active than just a comment.Quoted 3 lines
Oh I see,
usage_with_options()callsexit()inside so any code after that won't be executed. Hmm.. isn't it better to remove those codes then?
Quoted 8 lines
Not sure I follow. The patch does remove the code but it replaces it with
__builtin_unreachable()to basically state that the code here and below can never be reached. Do you mean remove the exit fromusage_with_options? Then we'd need to fix all the callers, which would be a larger patch. Perhaps it should beusage_with_options_and_exit()to make it clearer that the code doesn't return. I was after doing what was minimal for -Wunreachable-code but while trying to keep the code clear.
No, I meant we may not need the __builtin_unreachable() at the callsites.
Would it complain this code?
if (some_bad_option_use)
usage_with_options(...);/* normal code path */
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 64 lines · Namhyung Kim
Quoted 53 lines · Ian Rogers
Quoted 48 lines · Namhyung Kim
Quoted 37 lines · Ian Rogers
Quoted 31 lines · Namhyung Kim
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Quoted 4 lines
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace
exit()to__builtin_unreachable()- seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?Quoted 8 lines
Yep. The exit is incorrect (depending on your notion of correct, I'd go with clang's notion as they've had to consider this for a while) as it can never be executed. I've added the
__builtin_unreachable()to document that you can never get to that statement, as otherwise it can make the code readability harder with the code looking like it will fall through after calling something likeusage_with_options(which is noreturn). In unoptimized builds__builtin_unreachable()will fail if executed, so it is a bit more active than just a comment.Quoted 3 lines
Oh I see,
usage_with_options()callsexit()inside so any code after that won't be executed. Hmm.. isn't it better to remove those codes then?Quoted 8 lines
Not sure I follow. The patch does remove the code but it replaces it with
__builtin_unreachable()to basically state that the code here and below can never be reached. Do you mean remove the exit fromusage_with_options? Then we'd need to fix all the callers, which would be a larger patch. Perhaps it should beusage_with_options_and_exit()to make it clearer that the code doesn't return. I was after doing what was minimal for -Wunreachable-code but while trying to keep the code clear.
Quoted 8 lines
No, I meant we may not need the
__builtin_unreachable()at the callsites.Would it complain this code?
if (some_bad_option_use) usage_with_options(...);/* normal code path */
Right that would fix -Wunreachable, but the existing code would be:
if (some_bad_option_use) {
usage_with_options(...);
exit(..);
}
/* normal code path */Letting you know that "normal code path" couldn't be fallen into after usage_with_options(...). To make the behavior more obvious we could rename usage_with_options():
if (some_bad_option_use)
usage_with_options_and_exit(...);/* normal code path */
What I've done is:
if (some_bad_option_use) {
usage_with_options(...);
__builtin_unreachable();
}
/* normal code path */My reasoning is that usage_with_options() doesn't obviously on the face of it call exit and never return. To make that clear we could add a comment:
if (some_bad_option_use) {
usage_with_options(...);
/* usage_with_options never returns as it always calls exit */
}
/* normal code path */But my preference is to use __builtin_unreachable as that is the same as the comment but is also something the compiler can trap on were it not true.
Thanks,
Ian
Quoted 4 lines
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 109 lines · Ian Rogers
Quoted 64 lines · Namhyung Kim
Quoted 53 lines · Ian Rogers
Quoted 48 lines · Namhyung Kim
Quoted 37 lines · Ian Rogers
Quoted 31 lines · Namhyung Kim
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Quoted 13 lines · Namhyung Kim
Hi Ian,
Quoted 6 lines · Ian Rogers
Clang's unreachable code warning is able to catch bugs like the famous "goto fail" which gcc's unreachable code warning fails to warn about (it will complain about misleading indent). The changes here are sufficient to get perf building with clang with -Wunreachable code, but they don't really fix any bugs. Posting as an RFC to see if anyone things this is worth pursuing.
I'm not sure if it's useful and don't see what kind of bugs it can address. The proposed changes don't look like an improvement.
Quoted 11 lines
Quoted 11 lines
The goto fail case was in OpenSSL the code from a bad merge:
``` if (...) goto fail; goto fail; ```Meaning the fail path was always taken and checking on the non-fail code never executed. Newer GCCs will warn of this because of the "misleading indent" but clang won't. It is easy to imagine similar mistakes creeping in, so using compiler warnings to avoid the bug could be useful.
Quoted 4 lines
It doesn't look very convincing to me but it might be valuable in some rare cases. But the proposed changes - basically replace
exit()to__builtin_unreachable()- seem weird. Why is calling it a problem? I guess it already has some kind of annotation like "noreturn"?Quoted 8 lines
Yep. The exit is incorrect (depending on your notion of correct, I'd go with clang's notion as they've had to consider this for a while) as it can never be executed. I've added the
__builtin_unreachable()to document that you can never get to that statement, as otherwise it can make the code readability harder with the code looking like it will fall through after calling something likeusage_with_options(which is noreturn). In unoptimized builds__builtin_unreachable()will fail if executed, so it is a bit more active than just a comment.Quoted 3 lines
Oh I see,
usage_with_options()callsexit()inside so any code after that won't be executed. Hmm.. isn't it better to remove those codes then?Quoted 8 lines
Not sure I follow. The patch does remove the code but it replaces it with
__builtin_unreachable()to basically state that the code here and below can never be reached. Do you mean remove the exit fromusage_with_options? Then we'd need to fix all the callers, which would be a larger patch. Perhaps it should beusage_with_options_and_exit()to make it clearer that the code doesn't return. I was after doing what was minimal for -Wunreachable-code but while trying to keep the code clear.Quoted 8 lines
No, I meant we may not need the
__builtin_unreachable()at the callsites.Would it complain this code?
if (some_bad_option_use) usage_with_options(...);/* normal code path */
Right that would fix -Wunreachable, but the existing code would be:
if (some_bad_option_use) { usage_with_options(...); exit(..); } /* normal code path */Letting you know that "normal code path" couldn't be fallen into after
usage_with_options(...). To make the behavior more obvious we could renameusage_with_options():if (some_bad_option_use) usage_with_options_and_exit(...);/* normal code path */
What I've done is:
if (some_bad_option_use) { usage_with_options(...); __builtin_unreachable(); } /* normal code path */My reasoning is that
usage_with_options()doesn't obviously on the face of it call exit and never return. To make that clear we could add a comment:if (some_bad_option_use) { usage_with_options(...); /* usage_with_options never returns as it always calls exit */ } /* normal code path */
Quoted 3 lines
But my preference is to use
__builtin_unreachableas that is the same as the comment but is also something the compiler can trap on were it not true.
I see, thanks for the explanation. That part looks ok then.
Thanks,
Namhyung
On Fri, 30 May 2025 15:33:33 -0700
Namhyung Kim <[email protected]> wrote:
...
Quoted 26 lines
Quoted 26 lines
Quoted 8 lines
No, I meant we may not need the
__builtin_unreachable()at the callsites.Would it complain this code?
if (some_bad_option_use) usage_with_options(...);/* normal code path */
Right that would fix -Wunreachable, but the existing code would be:
if (some_bad_option_use) { usage_with_options(...); exit(..); } /* normal code path */Letting you know that "normal code path" couldn't be fallen into after
usage_with_options(...). To make the behavior more obvious we could renameusage_with_options():if (some_bad_option_use) usage_with_options_and_exit(...);/* normal code path */
Quoted 7 lines
Quoted 7 lines
What I've done is:
if (some_bad_option_use) { usage_with_options(...); __builtin_unreachable(); } /* normal code path */
Isn't that just wrong?
Doesn't __builtin_unreachable() tell the compiler the code can't be executed. In this case you want the opposite - something that the compiler will whinge about if it finds a path that does execute the code.
In terms of source code readability it is a also horrid.
It is pretty common for an application to have a lot of 'usage' errors and you want them to take as little vertical code space as possible. That is the whole reason why it is common for a usage() function to call exit() rather than return.
David
Quoted 21 lines
Quoted 11 lines
My reasoning is that
usage_with_options()doesn't obviously on the face of it call exit and never return. To make that clear we could add a comment:if (some_bad_option_use) { usage_with_options(...); /* usage_with_options never returns as it always calls exit */ } /* normal code path */Quoted 3 lines
But my preference is to use
__builtin_unreachableas that is the same as the comment but is also something the compiler can trap on were it not true.I see, thanks for the explanation. That part looks ok then.
Thanks,
Namhyung