Simplify linking against tools/perf code
Ian Rogers
reviewercover Simplify linking against tools/perf code
When fuzzing something like parse-events, having the main function in perf.c alongside global variables like input_name means that input_name must be redeclared with the fuzzer function's main. However, as the fuzzer is using the tools/perf code as a library this causes backward linking reference that the linker may warn about. Reorganize perf.c and perf.h to avoid potential backward references, or so that the declaration/definition locations are more consistent.
Ian Rogers (6):
perf ui: Move window resize signal functions perf usage: Move usage strings
perf header: Move perf_version_string declaration perf version: Use regular verbose flag perf util: Move input_name to util
perf util: Move perf_guest/host declarations
tools/perf/builtin-annotate.c | 2 +- tools/perf/builtin-buildid-list.c | 2 +- tools/perf/builtin-c2c.c | 2 +- tools/perf/builtin-data.c | 2 +- tools/perf/builtin-diff.c | 2 +- tools/perf/builtin-evlist.c | 2 +- tools/perf/builtin-help.c | 1 + tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-kvm.c | 1 + tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-mem.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/builtin-stat.c | 2 +- tools/perf/builtin-timechart.c | 2 +- tools/perf/builtin-version.c | 7 +++---- tools/perf/builtin.h | 3 --- tools/perf/perf.c | 27 +-------------------------- tools/perf/perf.h | 9 --------- tools/perf/ui/browsers/hists.c | 2 +- tools/perf/ui/hist.c | 2 +- tools/perf/ui/setup.c | 19 +++++++++++++++++++ tools/perf/ui/tui/setup.c | 1 - tools/perf/ui/ui.h | 3 +++ tools/perf/util/cs-etm.c | 1 + tools/perf/util/event.c | 2 +- tools/perf/util/evlist.c | 1 + tools/perf/util/header.h | 2 ++ tools/perf/util/parse-events.c | 2 +- tools/perf/util/session.c | 2 +- tools/perf/util/top.c | 2 +- tools/perf/util/usage.c | 6 ++++++ tools/perf/util/util.c | 2 ++ tools/perf/util/util.h | 8 ++++++++ 33 files changed, 66 insertions(+), 61 deletions(-)
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH 1/6 perf ui: Move window resize signal functions
Move under tools/perf/ui rather than in perf.c. This simplifies linking a main function when perf.c isn't present.
--- tools/perf/perf.c | 18 ------------------ tools/perf/perf.h | 2 -- tools/perf/ui/setup.c | 19 +++++++++++++++++++ tools/perf/ui/tui/setup.c | 1 - tools/perf/ui/ui.h | 3 +++ 5 files changed, 22 insertions(+), 21 deletions(-)
tools/perf/perf.c +0 -18
@@ -432,24 +432,6 @@ static int run_argv(int *argcp, const char ***argv)return 0 ; } -static void pthread__block_sigwinch (void ) -{ -sigset_t set; - -sigemptyset (&set); -sigaddset (&set,SIGWINCH ); -pthread_sigmask (SIG_BLOCK , &set,NULL ); -} - -void pthread__unblock_sigwinch (void ) -{ -sigset_t set; - -sigemptyset (&set); -sigaddset (&set,SIGWINCH ); -pthread_sigmask (SIG_UNBLOCK , &set,NULL ); -} -static int libperf_print (enum libperf_print_level level,const char *fmt,va_list ap) {
tools/perf/perf.h +0 -2
@@ -12,8 +12,6 @@ extern const char *input_name;extern bool perf_host, perf_guest;extern const char perf_version_string[]; -void pthread__unblock_sigwinch (void ); -enum perf_affinity {PERF_AFFINITY_SYS =0 ,PERF_AFFINITY_NODE ,
tools/perf/ui/setup.c +19 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <dlfcn.h> +#include <signal.h> #include <unistd.h> #include <subcmd/pager.h>
@@ -120,3 +121,21 @@ void exit_browser(bool wait_for_ok) }mutex_destroy (&ui__lock); } + +void pthread__block_sigwinch (void ) +{ +sigset_t set; + +sigemptyset (&set); +sigaddset (&set,SIGWINCH ); +pthread_sigmask (SIG_BLOCK , &set,NULL ); +} + +void pthread__unblock_sigwinch (void ) +{ +sigset_t set; + +sigemptyset (&set); +sigaddset (&set,SIGWINCH ); +pthread_sigmask (SIG_UNBLOCK , &set,NULL ); +}
tools/perf/ui/tui/setup.c +0 -1
@@ -9,7 +9,6 @@#endif #include "../../util/debug.h" -#include "../../perf.h" #include "../browser.h" #include "../helpline.h" #include "../ui.h"
tools/perf/ui/ui.h +3 -0
@@ -31,4 +31,7 @@ struct option;int stdio__config_color (const struct option *opt,const char *mode,int unset); +void pthread__block_sigwinch (void ); +void pthread__unblock_sigwinch (void ); +#endif /* _PERF_UI_H_ */
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH 2/6 perf usage: Move usage strings
In reply to Ian RogersThis simplifies linking a main function when perf.c isn't present.
--- tools/perf/builtin-help.c | 1 + tools/perf/builtin.h | 3 --- tools/perf/perf.c | 6 ------ tools/perf/util/usage.c | 6 ++++++ tools/perf/util/util.h | 3 +++ 5 files changed, 10 insertions(+), 9 deletions(-)
tools/perf/builtin-help.c +1 -0
@@ -14,6 +14,7 @@#include <subcmd/run-command.h> #include <subcmd/help.h> #include "util/debug.h" +#include "util/util.h" #include <linux/kernel.h> #include <linux/string.h> #include <linux/zalloc.h>
tools/perf/builtin.h +0 -3
@@ -2,9 +2,6 @@#ifndef BUILTIN_H #define BUILTIN_H -extern const char perf_usage_string[]; -extern const char perf_more_info_string[]; -void list_common_cmds_help (void );const char *help_unknown_cmd (const char *cmd);
tools/perf/perf.c +0 -6
@@ -39,12 +39,6 @@#include <linux/string.h> #include <linux/zalloc.h> -const char perf_usage_string[] = -"perf [--version] [--help] [OPTIONS] COMMAND [ARGS]" ; - -const char perf_more_info_string[] = -"See 'perf help COMMAND' for more information on a specific command." ; -static int use_pager =-1 ;const char *input_name;
tools/perf/util/usage.c +6 -0
@@ -12,6 +12,12 @@#include <stdlib.h> #include <linux/compiler.h> +const char perf_usage_string[] = +"perf [--version] [--help] [OPTIONS] COMMAND [ARGS]" ; + +const char perf_more_info_string[] = +"See 'perf help COMMAND' for more information on a specific command." ; +static __noreturn void usage_builtin (const char *err) {fprintf (stderr,"\n Usage: %s\n" , err);
tools/perf/util/util.h +3 -0
@@ -15,6 +15,9 @@#include <internal/cpumap.h> #endif +extern const char perf_usage_string[]; +extern const char perf_more_info_string[]; +/* General helper functions */ void usage (const char *err)__noreturn ; void die(const char *err, ...) __noreturn __printf(1, 2);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH 3/6 perf header: Move perf_version_string declaration
In reply to Ian RogersMove to match the definition in header.c.
--- tools/perf/builtin-version.c | 2 +- tools/perf/perf.h | 1 - tools/perf/util/header.h | 2 ++ 3 files changed, 3 insertions(+), 2 deletions(-)
tools/perf/builtin-version.c +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include "builtin.h" -#include "perf.h" #include "color.h" +#include "util/header.h" #include <tools/config.h> #include <stdbool.h> #include <stdio.h>
tools/perf/perf.h +0 -1
@@ -10,7 +10,6 @@extern const char *input_name;extern bool perf_host, perf_guest; -extern const char perf_version_string[];enum perf_affinity {PERF_AFFINITY_SYS =0 ,
tools/perf/util/header.h +2 -0
@@ -115,6 +115,8 @@ struct perf_session;struct perf_tool ;union perf_event ; +extern const char perf_version_string[]; +int perf_session__read_header (struct perf_session *session,int repipe_fd); int perf_session__write_header(struct perf_session *session, struct evlist *evlist,
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH 4/6 perf version: Use regular verbose flag
In reply to Ian RogersRemove additional version_verbose flag by using the existing verbose variable.
--- tools/perf/builtin-version.c | 5 ++--- tools/perf/perf.c | 2 +- tools/perf/perf.h | 1 - 3 files changed, 3 insertions(+), 5 deletions(-)
tools/perf/builtin-version.c +2 -3
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include "builtin.h" #include "color.h" +#include "util/debug.h" #include "util/header.h" #include <tools/config.h> #include <stdbool.h>
@@ -8,8 +9,6 @@#include <string.h> #include <subcmd/parse-options.h> -int version_verbose; -struct version {bool build_options; };
@@ -92,7 +91,7 @@ int cmd_version(int argc, const char **argv)printf ("perf version %s\n" , perf_version_string); -if (version.build_options || version_verbose ==1 ) +if (version.build_options || verbose >0 )library_status ();return 0 ;
tools/perf/perf.c +1 -1
@@ -212,7 +212,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)if (!strcmp (cmd,"-vv" )) { (*argv)[0 ] ="version" ; - version_verbose =1 ; + verbose =1 ;break ; }
tools/perf/perf.h +0 -1
@@ -18,5 +18,4 @@ enum perf_affinity {PERF_AFFINITY_MAX }; -extern int version_verbose;#endif
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH 5/6 perf util: Move input_name to util
In reply to Ian Rogersinput_name is the name of the input perf.data file, but having it in perf.c makes it difficult to link without perf.c. Move it to util.
--- tools/perf/builtin-annotate.c | 2 +- tools/perf/builtin-buildid-list.c | 2 +- tools/perf/builtin-c2c.c | 2 +- tools/perf/builtin-data.c | 2 +- tools/perf/builtin-evlist.c | 2 +- tools/perf/builtin-kmem.c | 2 +- tools/perf/builtin-kwork.c | 2 +- tools/perf/builtin-mem.c | 2 +- tools/perf/builtin-sched.c | 2 +- tools/perf/builtin-stat.c | 2 +- tools/perf/builtin-timechart.c | 2 +- tools/perf/perf.c | 1 - tools/perf/perf.h | 1 - tools/perf/ui/browsers/hists.c | 2 +- tools/perf/util/util.c | 2 ++ tools/perf/util/util.h | 2 ++ 16 files changed, 16 insertions(+), 14 deletions(-)
tools/perf/builtin-annotate.c +1 -1
@@ -15,7 +15,6 @@#include <linux/zalloc.h> #include "util/symbol.h" -#include "perf.h" #include "util/debug.h" #include "util/evlist.h"
@@ -36,6 +35,7 @@#include "util/block-range.h" #include "util/map_symbol.h" #include "util/branch.h" +#include "util/util.h" #include <dlfcn.h> #include <errno.h>
tools/perf/builtin-buildid-list.c +1 -1
@@ -8,7 +8,6 @@* Copyright (C) 2009, Arnaldo Carvalho de Melo <[email protected]> */ #include "builtin.h" -#include "perf.h" #include "util/build-id.h" #include "util/debug.h" #include "util/dso.h"
@@ -18,6 +17,7 @@#include "util/session.h" #include "util/symbol.h" #include "util/data.h" +#include "util/util.h" #include <errno.h> #include <inttypes.h> #include <linux/err.h>
tools/perf/builtin-c2c.c +1 -1
@@ -41,10 +41,10 @@#include "symbol.h" #include "ui/ui.h" #include "ui/progress.h" -#include "../perf.h" #include "pmu.h" #include "pmu-hybrid.h" #include "string2.h" +#include "util/util.h" struct c2c_hists {struct hists hists;
tools/perf/builtin-data.c +1 -1
@@ -3,10 +3,10 @@#include <stdio.h> #include <string.h> #include "builtin.h" -#include "perf.h" #include "debug.h" #include <subcmd/parse-options.h> #include "data-convert.h" +#include "util/util.h" typedef int (*data_cmd_fn_t )(int argc,const char **argv);
tools/perf/builtin-evlist.c +1 -1
@@ -7,7 +7,6 @@#include <linux/list.h> -#include "perf.h" #include "util/evlist.h" #include "util/evsel.h" #include "util/evsel_fprintf.h"
@@ -18,6 +17,7 @@#include "util/debug.h" #include <linux/err.h> #include "util/tool.h" +#include "util/util.h" static int process_header_feature (struct perf_session *session__maybe_unused ,union perf_event *event__maybe_unused )
tools/perf/builtin-kmem.c +1 -1
@@ -1,6 +1,5 @@// SPDX-License-Identifier: GPL-2.0 #include "builtin.h" -#include "perf.h" #include "util/dso.h" #include "util/evlist.h"
@@ -24,6 +23,7 @@#include "util/debug.h" #include "util/string2.h" +#include "util/util.h" #include <linux/kernel.h> #include <linux/numa.h>
tools/perf/builtin-kwork.c +1 -1
@@ -6,7 +6,6 @@*/ #include "builtin.h" -#include "perf.h" #include "util/data.h" #include "util/evlist.h"
@@ -20,6 +19,7 @@#include "util/string2.h" #include "util/callchain.h" #include "util/evsel_fprintf.h" +#include "util/util.h" #include <subcmd/pager.h> #include <subcmd/parse-options.h>
tools/perf/builtin-mem.c +1 -1
@@ -4,7 +4,6 @@#include <sys/stat.h> #include <unistd.h> #include "builtin.h" -#include "perf.h" #include <subcmd/parse-options.h> #include "util/auxtrace.h"
@@ -22,6 +21,7 @@#include "util/pmu-hybrid.h" #include "util/sample.h" #include "util/string2.h" +#include "util/util.h" #include <linux/err.h> #define MEM_OPERATION_LOAD 0x1
tools/perf/builtin-sched.c +1 -1
@@ -1,6 +1,5 @@// SPDX-License-Identifier: GPL-2.0 #include "builtin.h" -#include "perf.h" #include "perf-sys.h" #include "util/cpumap.h"
@@ -27,6 +26,7 @@#include "util/debug.h" #include "util/event.h" +#include "util/util.h" #include <linux/kernel.h> #include <linux/log2.h>
tools/perf/builtin-stat.c +1 -1
@@ -41,7 +41,6 @@*/ #include "builtin.h" -#include "perf.h" #include "util/cgroup.h" #include <subcmd/parse-options.h> #include "util/parse-events.h"
@@ -71,6 +70,7 @@#include "util/bpf_counter.h" #include "util/iostat.h" #include "util/pmu-hybrid.h" +#include "util/util.h" #include "asm/bug.h" #include <linux/time64.h>
tools/perf/builtin-timechart.c +1 -1
@@ -24,7 +24,6 @@#include "util/thread.h" #include "util/callchain.h" -#include "perf.h" #include "util/header.h" #include <subcmd/pager.h> #include <subcmd/parse-options.h>
@@ -37,6 +36,7 @@#include "util/debug.h" #include "util/string2.h" #include "util/tracepoint.h" +#include "util/util.h" #include <linux/err.h> #include <traceevent/event-parse.h>
tools/perf/perf.c +0 -1
@@ -40,7 +40,6 @@#include <linux/zalloc.h> static int use_pager =-1 ; -const char *input_name;struct cmd_struct {const char *cmd;
tools/perf/perf.h +0 -1
@@ -8,7 +8,6 @@#define MAX_NR_CPUS 2048#endif -extern const char *input_name;extern bool perf_host, perf_guest;enum perf_affinity {
tools/perf/ui/browsers/hists.c +1 -1
@@ -29,8 +29,8 @@#include "../../util/top.h" #include "../../util/thread.h" #include "../../util/block-info.h" +#include "../../util/util.h" #include "../../arch/common.h" -#include "../../perf.h" #include "../browsers/hists.h" #include "../helpline.h"
tools/perf/util/util.c +2 -0
@@ -28,6 +28,8 @@* XXX We need to find a better place for these things... */ +const char *input_name; +bool perf_singlethreaded = true;void perf_set_singlethreaded (void )
tools/perf/util/util.h +2 -0
@@ -18,6 +18,8 @@extern const char perf_usage_string[];extern const char perf_more_info_string[]; +extern const char *input_name; +/* General helper functions */ void usage (const char *err)__noreturn ; void die(const char *err, ...) __noreturn __printf(1, 2);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Ian Rogers
reviewerPATCH 6/6 perf util: Move perf_guest/host declarations
In reply to Ian RogersThe definitions are in util.c so move the declarations to match.
--- tools/perf/builtin-diff.c | 2 +- tools/perf/builtin-kvm.c | 1 + tools/perf/perf.h | 4 ---- tools/perf/ui/hist.c | 2 +- tools/perf/util/cs-etm.c | 1 + tools/perf/util/event.c | 2 +- tools/perf/util/evlist.c | 1 + tools/perf/util/parse-events.c | 2 +- tools/perf/util/session.c | 2 +- tools/perf/util/top.c | 2 +- tools/perf/util/util.h | 3 +++ 11 files changed, 12 insertions(+), 10 deletions(-)
tools/perf/builtin-diff.c +1 -1
@@ -6,7 +6,6 @@* DSOs and symbol information, sort them and produce a diff. */ #include "builtin.h" -#include "perf.h" #include "util/debug.h" #include "util/event.h"
@@ -26,6 +25,7 @@#include "util/spark.h" #include "util/block-info.h" #include "util/stream.h" +#include "util/util.h" #include <linux/err.h> #include <linux/zalloc.h> #include <subcmd/pager.h>
tools/perf/builtin-kvm.c +1 -0
@@ -23,6 +23,7 @@#include "util/data.h" #include "util/ordered-events.h" #include "util/kvm-stat.h" +#include "util/util.h" #include "ui/browsers/hists.h" #include "ui/progress.h" #include "ui/ui.h"
tools/perf/perf.h +0 -4
@@ -2,14 +2,10 @@#ifndef _PERF_PERF_H#define _PERF_PERF_H -#include <stdbool.h> -#ifndef MAX_NR_CPUS #define MAX_NR_CPUS 2048#endif -extern bool perf_host, perf_guest; -enum perf_affinity {PERF_AFFINITY_SYS =0 , PERF_AFFINITY_NODE,
tools/perf/ui/hist.c +1 -1
@@ -11,7 +11,7 @@#include "../util/sort.h" #include "../util/evsel.h" #include "../util/evlist.h" -#include "../perf.h" +#include "../util/util.h" /* hist period print (hpp) functions */
tools/perf/util/cs-etm.c +1 -0
@@ -38,6 +38,7 @@#include "tsc.h" #include <tools/libc_compat.h> #include "util/synthetic-events.h" +#include "util/util.h" struct cs_etm_auxtrace {struct auxtrace auxtrace;
tools/perf/util/event.c +1 -1
@@ -33,7 +33,7 @@#include "bpf-event.h" #include "print_binary.h" #include "tool.h" -#include "../perf.h" +#include "util.h" static const char *perf_event__names[] = { [0 ] ="TOTAL" ,
tools/perf/util/evlist.c +1 -0
@@ -32,6 +32,7 @@#include "util/pmu.h" #include "util/sample.h" #include "util/bpf-filter.h" +#include "util/util.h" #include <signal.h> #include <unistd.h> #include <sched.h>
tools/perf/util/parse-events.c +1 -1
@@ -25,10 +25,10 @@#include "util/parse-branch-options.h" #include "util/evsel_config.h" #include "util/event.h" -#include "perf.h" #include "util/parse-events-hybrid.h" #include "util/pmu-hybrid.h" #include "util/bpf-filter.h" +#include "util/util.h" #include "tracepoint.h" #include "thread_map.h"
tools/perf/util/session.c +1 -1
@@ -33,7 +33,7 @@#include "stat.h" #include "tsc.h" #include "ui/progress.h" -#include "../perf.h" +#include "util.h" #include "arch/common.h" #include "units.h" #include <internal/lib.h>
tools/perf/util/top.c +1 -1
@@ -11,7 +11,7 @@#include "parse-events.h" #include "symbol.h" #include "top.h" -#include "../perf.h" +#include "util.h" #include <inttypes.h> #define SNPRINTF(buf, size, fmt, args...) \
tools/perf/util/util.h +3 -0
@@ -20,6 +20,9 @@ extern const char perf_more_info_string[];extern const char *input_name; +extern bool perf_host; +extern bool perf_guest; +/* General helper functions */ void usage (const char *err)__noreturn ; void die(const char *err, ...) __noreturn __printf(1, 2);
Signature · 1 line
2.40.0.348.gf938b09366-goog
Adrian Hunter
reviewerQuoted 9 lines · Ian Rogers
When fuzzing something like parse-events, having the main function in
perf.calongside global variables likeinput_namemeans thatinput_namemust be redeclared with the fuzzer function's main. However, as the fuzzer is using thetools/perfcode as a library this causes backward linking reference that the linker may warn about. Reorganizeperf.candperf.hto avoid potential backward references, or so that the declaration/definition locations are more consistent.
Seems like it could be a pain to maintain.
Did you consider just adding:
tools/perf/perf.c +2 -0
@@ -456,6 +456,7 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); } +#ifndef CUSTOM_MAIN int main (int argc,const char **argv) {int err;
@@ -576,3 +577,4 @@ int main(int argc, const char **argv) out: return 1; } +#endif
Ian Rogers
reviewerQuoted 30 lines · Adrian Hunter
Quoted 9 lines · Ian Rogers
When fuzzing something like parse-events, having the main function in
perf.calongside global variables likeinput_namemeans thatinput_namemust be redeclared with the fuzzer function's main. However, as the fuzzer is using thetools/perfcode as a library this causes backward linking reference that the linker may warn about. Reorganizeperf.candperf.hto avoid potential backward references, or so that the declaration/definition locations are more consistent.Seems like it could be a pain to maintain.
Did you consider just adding:
tools/perf/perf.c +1
-0@@ -456,6 +456,7 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); } +#ifndef CUSTOM_MAIN int main (int argc,const char **argv) {int err;
tools/perf/perf.c int main(int argc, const char **argv) · line 580
Quoted 4 lines
tools/perf/perf.c +1
-0@@ -576,3 +577,4 @@ int main(int argc, const char **argv) out: return 1; } +#endif
It's possible. Would need to make the static functions not warn about being declared and not used. I still think that just aligning definitions and declarations yields the most expected code and will lead to fewer problems in the long run.
Thanks,
Ian
Adrian Hunter
reviewerQuoted 25 lines · Ian Rogers
Quoted 24 lines · Adrian Hunter
Quoted 9 lines · Ian Rogers
When fuzzing something like parse-events, having the main function in
perf.calongside global variables likeinput_namemeans thatinput_namemust be redeclared with the fuzzer function's main. However, as the fuzzer is using thetools/perfcode as a library this causes backward linking reference that the linker may warn about. Reorganizeperf.candperf.hto avoid potential backward references, or so that the declaration/definition locations are more consistent.Seems like it could be a pain to maintain.
Did you consider just adding:
tools/perf/perf.c +0
-0@@ -456,6 +456,7 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); }
Quoted 15 lines
tools/perf/perf.c int main(int argc, const char **argv) · line 580
Quoted 10 lines
tools/perf/perf.c +2
-0+#ifndef CUSTOM_MAIN int main (int argc,const char **argv) {int err;@@ -576,3 +577,4 @@ int main(int argc, const char **argv) out: return 1; } +#endif It's possible. Would need to make the static functions not warn about being declared and not used. I still think that just aligning definitions and declarations yields the most expected code and will lead to fewer problems in the long run.
Making perf source dependent on an unknown derivative makes things more complicated.
If you are not going to contribute it to perf, then a suggestion is along the lines of the following:
tools/perf/perf.c +0 -0
@@ -456,7 +456,18 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); }
$ git diff
+#ifdef CUSTOM_MAIN
+int main(void)
+{
+ printf("This is not perf\n");
+ return 0;
+}
+
+int perf_main(int argc, const char **argv);
+int perf_main(int argc, const char **argv)
+#else
int main(int argc, const char **argv)
+#endif
{
int err;
const char *cmd;
$ make EXTRA_CFLAGS="-DCUSTOM_MAIN" NO_BPF_SKEL=1 -C tools/perf >/dev/null
Warning: Kernel ABI header at 'tools/include/uapi/linux/in.h' differs from latest version at 'include/uapi/linux/in.h'
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h'
Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/perf_regs.h' differs from latest version at 'arch/arm64/include/uapi/asm/perf_regs.h'
Warning: Kernel ABI header at 'tools/include/linux/coresight-pmu.h' differs from latest version at 'include/linux/coresight-pmu.h'
Makefile.config:587: No sys/sdt.h found, no SDT events are defined, please install systemtap-sdt-devel or systemtap-sdt-dev
Makefile.config:805: Missing perl devel files. Disabling perl scripting support, please install perl-ExtUtils-Embed/libperl-dev
Makefile.config:1046: No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev
Makefile.config:1075: No alternatives command found, you need to set JDIR= to point to the root of your Java directory
Makefile.config:1137: libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev
$ tools/perf/perf version
This is not perfIan Rogers
reviewerQuoted 43 lines · Adrian Hunter
Quoted 25 lines · Ian Rogers
Quoted 24 lines · Adrian Hunter
Quoted 9 lines · Ian Rogers
When fuzzing something like parse-events, having the main function in
perf.calongside global variables likeinput_namemeans thatinput_namemust be redeclared with the fuzzer function's main. However, as the fuzzer is using thetools/perfcode as a library this causes backward linking reference that the linker may warn about. Reorganizeperf.candperf.hto avoid potential backward references, or so that the declaration/definition locations are more consistent.Seems like it could be a pain to maintain.
Did you consider just adding:
tools/perf/perf.c +0
-0@@ -456,6 +456,7 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); }Quoted 15 lines
tools/perf/perf.c int main(int argc, const char **argv) · line 580
Quoted 10 lines
tools/perf/perf.c +2
-0+#ifndef CUSTOM_MAIN int main (int argc,const char **argv) {int err;@@ -576,3 +577,4 @@ int main(int argc, const char **argv) out: return 1; } +#endif It's possible. Would need to make the static functions not warn about being declared and not used. I still think that just aligning definitions and declarations yields the most expected code and will lead to fewer problems in the long run.
Quoted 5 lines
Making perf source dependent on an unknown derivative makes things more complicated.
If you are not going to contribute it to perf, then a suggestion is along the lines of the following:
There's not an issue with contribution, most of the fuzzers are very simple. For example, here is the coresight fuzzer we run:
```
#include <fuzzer/FuzzedDataProvider.h>
extern "C" {
#include "tools/perf/util/cs-etm-decoder/cs-etm-decoder.h"
}
static void null_packet_dump(const char *) {}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fdp(data, size);
int num_cpu = fdp.ConsumeIntegralInRange(1, 1024);
struct cs_etm_decoder_params d_params;
std::unique_ptr<struct cs_etm_trace_params[]>
t_params(new cs_etm_trace_params[num_cpu]);
for(int i=0; i < num_cpu; ++i) {
t_params[i].protocol =
fdp.ConsumeIntegralInRange(static_cast<int>(CS_ETM_PROTO_ETMV3)
,
static_cast<int>(CS_ETM_PROTO_PTM));
fdp.ConsumeData(&t_params[i].etmv4, sizeof(struct cs_etmv4_trace_params));
}
d_params.packet_printer = null_packet_dump;
d_params.operation = CS_ETM_OPERATION_DECODE;
d_params.data = NULL;
d_params.formatted = true;
d_params.fsyncs = false;
d_params.hsyncs = false;
d_params.frame_aligned = true;
std::unique_ptr<struct cs_etm_decoder, decltype(&cs_etm_decoder__free)>
decoder(cs_etm_decoder__new(num_cpu, &d_params, t_params.get()),
cs_etm_decoder__free);
if (decoder == nullptr)
return 0;
do {
size_t consumed;
uint8_t buf[1024];
size_t len = fdp.ConsumeData(buf, sizeof(buf));
int ret = cs_etm_decoder__process_data_block(
decoder.get(), 0, buf, len, &consumed);
if (ret)
return 0;
} while (fdp.remaining_bytes() > 0);
return 0;
}
```Most of the code is boiler plate around a single function call. The issues are build support, where to put the fuzzer generated test corpus, the code is C++, etc.
Quoted 28 lines
tools/perf/perf.c +0
-0@@ -456,7 +456,18 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); }$ git difftools/perf/perf.c +11
-0+#ifdef CUSTOM_MAIN +int main (void ) +{ +printf ("This is not perf\n" ); +return 0 ; +} + +int perf_main (int argc,const char **argv); +int perf_main(int argc, const char **argv) +#else int main(int argc, const char **argv) +#endif {int err;const char *cmd;$ make EXTRA_CFLAGS="-DCUSTOM_MAIN" NO_BPF_SKEL=1 -C tools/perf >/dev/null Warning: Kernel ABI header at 'tools/include/uapi/linux/in.h' differs from latest version at 'include/uapi/linux/in.h' Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h' Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/perf_regs.h' differs from latest version at 'arch/arm64/include/uapi/asm/perf_regs.h'
Quoted 9 lines
Warning: Kernel ABI header at 'tools/include/linux/coresight-pmu.h' differs from latest version at 'include/linux/coresight-pmu.h' Makefile.config:587: No sys/sdt.h found, no SDT events are defined, please install systemtap-sdt-devel or systemtap-sdt-dev Makefile.config:805: Missing perl devel files. Disabling perl scripting support, please install perl-ExtUtils-Embed/libperl-dev Makefile.config:1046: No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev Makefile.config:1075: No alternatives command found, you need to set JDIR= to point to the root of your Java directory Makefile.config:1137: libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev $ tools/perf/perf version This is not perf
Sure, I'm aware of how the #ifdef would function. What I'm saying is that even with a CUSTOM_MAIN ifdef, which wouldn't be necessary with my patches as you can drop the object file, the refactoring in the patches still makes sense.
perf ui: Move window resize signal functions
It isn't clear why these UI functions should be in perf.c given window resizing is a UI issue.
perf usage: Move usage strings
The usage strings were in builtin.h and not perf.h, so the declaration/definition didn't align. util.h declares usage(), although the definition is in usage.c, the variables are moved to match this.
perf header: Move perf_version_string declaration
perf_version_string is defined in header.c and so declaring it in header.h rather than perf.h is consistent.
perf version: Use regular verbose flag
Just remove an unnecessary global by repurposing the existing global for the job.
perf util: Move input_name to util
There's no reference to input_name in perf.c and so util.c is as sensible a location. Having this be a global is something of an encapsulation fail, but cleaning that up wasn't the point of these patches. The variable is used in util, so reaching up a directory to get it feels somewhat unnatural.
perf util: Move perf_guest/host declarations
As with input_name.
Thanks,
Ian
Adrian Hunter
reviewerQuoted 146 lines · Ian Rogers
Quoted 43 lines · Adrian Hunter
Quoted 25 lines · Ian Rogers
Quoted 24 lines · Adrian Hunter
Quoted 9 lines · Ian Rogers
When fuzzing something like parse-events, having the main function in
perf.calongside global variables likeinput_namemeans thatinput_namemust be redeclared with the fuzzer function's main. However, as the fuzzer is using thetools/perfcode as a library this causes backward linking reference that the linker may warn about. Reorganizeperf.candperf.hto avoid potential backward references, or so that the declaration/definition locations are more consistent.Seems like it could be a pain to maintain.
Did you consider just adding:
tools/perf/perf.c +0
-0@@ -456,6 +456,7 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); }Quoted 15 lines
tools/perf/perf.c int main(int argc, const char **argv) · line 580
Quoted 10 lines
tools/perf/perf.c +2
-0+#ifndef CUSTOM_MAIN int main (int argc,const char **argv) {int err;@@ -576,3 +577,4 @@ int main(int argc, const char **argv) out: return 1; } +#endif It's possible. Would need to make the static functions not warn about being declared and not used. I still think that just aligning definitions and declarations yields the most expected code and will lead to fewer problems in the long run.
Quoted 5 lines
Making perf source dependent on an unknown derivative makes things more complicated.
If you are not going to contribute it to perf, then a suggestion is along the lines of the following:
There's not an issue with contribution, most of the fuzzers are very simple. For example, here is the coresight fuzzer we run:
``` #include <fuzzer/FuzzedDataProvider.h> extern "C" { #include "tools/perf/util/cs-etm-decoder/cs-etm-decoder.h" } static void null_packet_dump(const char *) {} extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FuzzedDataProvider fdp(data, size); int num_cpu = fdp.ConsumeIntegralInRange(1, 1024); struct cs_etm_decoder_params d_params; std::unique_ptr<struct cs_etm_trace_params[]> t_params(new cs_etm_trace_params[num_cpu]); for(int i=0; i < num_cpu; ++i) { t_params[i].protocol = fdp.ConsumeIntegralInRange(static_cast<int>(CS_ETM_PROTO_ETMV3) , static_cast<int>(CS_ETM_PROTO_PTM)); fdp.ConsumeData(&t_params[i].etmv4, sizeof(struct cs_etmv4_trace_params)); } d_params.packet_printer = null_packet_dump; d_params.operation = CS_ETM_OPERATION_DECODE; d_params.data = NULL; d_params.formatted = true; d_params.fsyncs = false; d_params.hsyncs = false; d_params.frame_aligned = true; std::unique_ptr<struct cs_etm_decoder, decltype(&cs_etm_decoder__free)> decoder(cs_etm_decoder__new(num_cpu, &d_params, t_params.get()), cs_etm_decoder__free); if (decoder == nullptr) return 0; do { size_t consumed; uint8_t buf[1024]; size_t len = fdp.ConsumeData(buf, sizeof(buf)); int ret = cs_etm_decoder__process_data_block( decoder.get(), 0, buf, len, &consumed); if (ret) return 0; } while (fdp.remaining_bytes() > 0); return 0; } ```Most of the code is boiler plate around a single function call. The issues are build support, where to put the fuzzer generated test corpus, the code is C++, etc.
Quoted 34 lines
tools/perf/perf.c +0
-0@@ -456,7 +456,18 @@ static int libperf_print(enum libperf_print_level level,return veprintf (level, verbose, fmt, ap); }$ git difftools/perf/perf.c +11
-0+#ifdef CUSTOM_MAIN +int main (void ) +{ +printf ("This is not perf\n" ); +return 0 ; +} + +int perf_main (int argc,const char **argv); +int perf_main(int argc, const char **argv) +#else int main(int argc, const char **argv) +#endif {int err;const char *cmd;$ make EXTRA_CFLAGS="-DCUSTOM_MAIN" NO_BPF_SKEL=1 -C tools/perf >/dev/null Warning: Kernel ABI header at 'tools/include/uapi/linux/in.h' differs from latest version at 'include/uapi/linux/in.h' Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h' Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/perf_regs.h' differs from latest version at 'arch/arm64/include/uapi/asm/perf_regs.h' Warning: Kernel ABI header at 'tools/include/linux/coresight-pmu.h' differs from latest version at 'include/linux/coresight-pmu.h' Makefile.config:587: No sys/sdt.h found, no SDT events are defined, please install systemtap-sdt-devel or systemtap-sdt-dev Makefile.config:805: Missing perl devel files. Disabling perl scripting support, please install perl-ExtUtils-Embed/libperl-dev Makefile.config:1046: No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev Makefile.config:1075: No alternatives command found, you need to set JDIR= to point to the root of your Java directory Makefile.config:1137: libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev
Quoted 8 lines
Quoted 3 lines
$ tools/perf/perf version This is not perfSure, I'm aware of how the #ifdef would function. What I'm saying is that even with a
CUSTOM_MAINifdef, which wouldn't be necessary with my patches as you can drop the object file, the refactoring in the patches still makes sense.
I am just going off what your commit messages say
Quoted 36 lines
perf ui: Move window resize signal functions
It isn't clear why these UI functions should be in
perf.cgiven window resizing is a UI issue.perf usage: Move usage strings
The usage strings were in
builtin.hand notperf.h, so the declaration/definition didn't align.util.hdeclaresusage(), although the definition is inusage.c, the variables are moved to match this.perf header: Move
perf_version_stringdeclaration
perf_version_stringis defined inheader.cand so declaring it inheader.hrather thanperf.his consistent.perf version: Use regular verbose flag
Just remove an unnecessary global by repurposing the existing global for the job.
perf util: Move
input_nameto utilThere's no reference to
input_nameinperf.cand soutil.cis as sensible a location. Having this be a global is something of an encapsulation fail, but cleaning that up wasn't the point of these patches. The variable is used in util, so reaching up a directory to get it feels somewhat unnatural.perf util: Move perf_guest/host declarations
As with
input_name.Thanks,
Ian