[PATCH 1/8] perf annotate: Introduce global annotation_options

From: Namhyung Kim
Date: Tue Nov 28 2023 - 12:55:01 EST


The annotation options are to control the behavior of objdump and the
output. It's basically used by perf annotate but perf report and perf
top can call it on TUI dynamically. But it doesn't need to have a copy
of annotation options in many places. As most of the work is done in
the util/annotate.c file, add a global variable and set/use it instead
of having their own copies.

Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
tools/perf/builtin-annotate.c | 43 +++++++++++++++++------------------
tools/perf/util/annotate.c | 3 +++
tools/perf/util/annotate.h | 2 ++
3 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index a9129b51d511..67b36a7a12e3 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -45,7 +45,6 @@
struct perf_annotate {
struct perf_tool tool;
struct perf_session *session;
- struct annotation_options opts;
#ifdef HAVE_SLANG_SUPPORT
bool use_tui;
#endif
@@ -318,9 +317,9 @@ static int hist_entry__tty_annotate(struct hist_entry *he,
struct perf_annotate *ann)
{
if (!ann->use_stdio2)
- return symbol__tty_annotate(&he->ms, evsel, &ann->opts);
+ return symbol__tty_annotate(&he->ms, evsel, &annotate_opts);

- return symbol__tty_annotate2(&he->ms, evsel, &ann->opts);
+ return symbol__tty_annotate2(&he->ms, evsel, &annotate_opts);
}

static void hists__find_annotations(struct hists *hists,
@@ -376,14 +375,14 @@ static void hists__find_annotations(struct hists *hists,
return;
}

- ret = annotate(he, evsel, &ann->opts, NULL);
+ ret = annotate(he, evsel, &annotate_opts, NULL);
if (!ret || !ann->skip_missing)
return;

/* skip missing symbols */
nd = rb_next(nd);
} else if (use_browser == 1) {
- key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
+ key = hist_entry__tui_annotate(he, evsel, NULL, &annotate_opts);

switch (key) {
case -1:
@@ -425,9 +424,9 @@ static int __cmd_annotate(struct perf_annotate *ann)
goto out;
}

- if (!ann->opts.objdump_path) {
+ if (!annotate_opts.objdump_path) {
ret = perf_env__lookup_objdump(&session->header.env,
- &ann->opts.objdump_path);
+ &annotate_opts.objdump_path);
if (ret)
goto out;
}
@@ -561,9 +560,9 @@ int cmd_annotate(int argc, const char **argv)
"file", "vmlinux pathname"),
OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
"load module symbols - WARNING: use only with -k and LIVE kernel"),
- OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
+ OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines,
"print matching source lines (may be slow)"),
- OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
+ OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path,
"Don't shorten the displayed pathnames"),
OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
"Skip symbols that cannot be annotated"),
@@ -574,15 +573,15 @@ int cmd_annotate(int argc, const char **argv)
OPT_CALLBACK(0, "symfs", NULL, "directory",
"Look for files with symbols relative to this directory",
symbol__config_symfs),
- OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
+ OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
"Interleave source code with assembly code (default)"),
- OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
+ OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
"Display raw encoding of assembly instructions (default)"),
OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
"Specify disassembler style (e.g. -M intel for intel syntax)"),
- OPT_STRING(0, "prefix", &annotate.opts.prefix, "prefix",
+ OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
"Add prefix to source file path names in programs (with --prefix-strip)"),
- OPT_STRING(0, "prefix-strip", &annotate.opts.prefix_strip, "N",
+ OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
"Strip first N entries of source file path name in programs (with --prefix)"),
OPT_STRING(0, "objdump", &objdump_path, "path",
"objdump binary to use for disassembly and annotations"),
@@ -601,7 +600,7 @@ int cmd_annotate(int argc, const char **argv)
OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
"'always' (default), 'never' or 'auto' only applicable to --stdio mode",
stdio__config_color, "always"),
- OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
+ OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
"Set percent type local/global-period/hits",
annotate_parse_percent_type),
OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
@@ -617,13 +616,13 @@ int cmd_annotate(int argc, const char **argv)
set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);

- annotation_options__init(&annotate.opts);
+ annotation_options__init(&annotate_opts);

ret = hists__init();
if (ret < 0)
return ret;

- annotation_config__init(&annotate.opts);
+ annotation_config__init(&annotate_opts);

argc = parse_options(argc, argv, options, annotate_usage, 0);
if (argc) {
@@ -638,13 +637,13 @@ int cmd_annotate(int argc, const char **argv)
}

if (disassembler_style) {
- annotate.opts.disassembler_style = strdup(disassembler_style);
- if (!annotate.opts.disassembler_style)
+ annotate_opts.disassembler_style = strdup(disassembler_style);
+ if (!annotate_opts.disassembler_style)
return -ENOMEM;
}
if (objdump_path) {
- annotate.opts.objdump_path = strdup(objdump_path);
- if (!annotate.opts.objdump_path)
+ annotate_opts.objdump_path = strdup(objdump_path);
+ if (!annotate_opts.objdump_path)
return -ENOMEM;
}
if (addr2line_path) {
@@ -653,7 +652,7 @@ int cmd_annotate(int argc, const char **argv)
return -ENOMEM;
}

- if (annotate_check_args(&annotate.opts) < 0)
+ if (annotate_check_args(&annotate_opts) < 0)
return -EINVAL;

#ifdef HAVE_GTK2_SUPPORT
@@ -734,7 +733,7 @@ int cmd_annotate(int argc, const char **argv)
#ifndef NDEBUG
perf_session__delete(annotate.session);
#endif
- annotation_options__exit(&annotate.opts);
+ annotation_options__exit(&annotate_opts);

return ret;
}
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 9a828dc601c7..77b78001b94d 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -57,6 +57,9 @@

#include <linux/ctype.h>

+/* global annotation options */
+struct annotation_options annotate_opts;
+
static regex_t file_lineno;

static struct ins_ops *ins__find(struct arch *arch, const char *name);
diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h
index b64a2be287b3..8c1a070725fa 100644
--- a/tools/perf/util/annotate.h
+++ b/tools/perf/util/annotate.h
@@ -105,6 +105,8 @@ struct annotation_options {
unsigned int percent_type;
};

+extern struct annotation_options annotate_opts;
+
enum {
ANNOTATION__OFFSET_JUMP_TARGETS = 1,
ANNOTATION__OFFSET_CALL,
--
2.43.0.rc1.413.gea7ed67945-goog