[1/2] perf script: Make it possible to see perf's kernel and module memory mappings

Message ID 20240202110130.3553-2-adrian.hunter@intel.com
State New
Headers
Series perf symbols: Slightly improve module file executable section mappings |

Commit Message

Adrian Hunter Feb. 2, 2024, 11:01 a.m. UTC
  Dump kmaps if verbose > 2.

Example:

  $ perf script -vvv 2>&1 >/dev/null | grep kvm.intel
  build id event received for /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko: 0691d75e10e72ebbbd45a44c59f6d00a5604badf [20]
  Map: 0-3a3 4f5d8 [kvm_intel].modinfo
  Map: 0-5240 5f280 [kvm_intel]__versions
  Map: 0-30 64 [kvm_intel].note.Linux
  Map: 0-14 644c0 [kvm_intel].orc_header
  Map: 0-5297 43680 [kvm_intel].rodata
  Map: 0-5bee 3b837 [kvm_intel].text.unlikely
  Map: 0-7e0 41430 [kvm_intel].noinstr.text
  Map: 0-2080 713c0 [kvm_intel].bss
  Map: 0-26 705c8 [kvm_intel].data..read_mostly
  Map: 0-5888 6a4c0 [kvm_intel].data
  Map: 0-22 70220 [kvm_intel].data.once
  Map: 0-40 705f0 [kvm_intel].data..percpu
  Map: 0-1685 41d20 [kvm_intel].init.text
  Map: 0-4b8 6fd60 [kvm_intel].init.data
  Map: 0-380 70248 [kvm_intel]__dyndbg
  Map: 0-8 70218 [kvm_intel].exit.data
  Map: 0-438 4f980 [kvm_intel]__param
  Map: 0-5f5 4ca0f [kvm_intel].rodata.str1.1
  Map: 0-3657 493b8 [kvm_intel].rodata.str1.8
  Map: 0-e0 70640 [kvm_intel].data..ro_after_init
  Map: 0-500 70ec0 [kvm_intel].gnu.linkonce.this_module
  Map: ffffffffc13a7000-ffffffffc1421000 a0 /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko

The example above shows how the module section mappings are all wrong
except for the main .text mapping at 0xffffffffc13a7000.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/builtin-script.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
  

Comments

Namhyung Kim Feb. 3, 2024, 1:50 a.m. UTC | #1
On Fri, Feb 2, 2024 at 3:01 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> Dump kmaps if verbose > 2.

Maybe we can add '--debug kmap' option rather than using an
arbitrary verbose level.

Thanks,
Namhyung

>
> Example:
>
>   $ perf script -vvv 2>&1 >/dev/null | grep kvm.intel
>   build id event received for /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko: 0691d75e10e72ebbbd45a44c59f6d00a5604badf [20]
>   Map: 0-3a3 4f5d8 [kvm_intel].modinfo
>   Map: 0-5240 5f280 [kvm_intel]__versions
>   Map: 0-30 64 [kvm_intel].note.Linux
>   Map: 0-14 644c0 [kvm_intel].orc_header
>   Map: 0-5297 43680 [kvm_intel].rodata
>   Map: 0-5bee 3b837 [kvm_intel].text.unlikely
>   Map: 0-7e0 41430 [kvm_intel].noinstr.text
>   Map: 0-2080 713c0 [kvm_intel].bss
>   Map: 0-26 705c8 [kvm_intel].data..read_mostly
>   Map: 0-5888 6a4c0 [kvm_intel].data
>   Map: 0-22 70220 [kvm_intel].data.once
>   Map: 0-40 705f0 [kvm_intel].data..percpu
>   Map: 0-1685 41d20 [kvm_intel].init.text
>   Map: 0-4b8 6fd60 [kvm_intel].init.data
>   Map: 0-380 70248 [kvm_intel]__dyndbg
>   Map: 0-8 70218 [kvm_intel].exit.data
>   Map: 0-438 4f980 [kvm_intel]__param
>   Map: 0-5f5 4ca0f [kvm_intel].rodata.str1.1
>   Map: 0-3657 493b8 [kvm_intel].rodata.str1.8
>   Map: 0-e0 70640 [kvm_intel].data..ro_after_init
>   Map: 0-500 70ec0 [kvm_intel].gnu.linkonce.this_module
>   Map: ffffffffc13a7000-ffffffffc1421000 a0 /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko
>
> The example above shows how the module section mappings are all wrong
> except for the main .text mapping at 0xffffffffc13a7000.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
> ---
>  tools/perf/builtin-script.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index b1f57401ff23..e764b319ef59 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -3806,6 +3806,16 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
>         return 0;
>  }
>
> +static void dump_kmaps(struct perf_session *session)
> +{
> +       int save_verbose = verbose;
> +
> +       pr_debug("Kernel and module maps:\n");
> +       verbose = 0; /* Suppress verbose to print a summary only */
> +       maps__fprintf(machine__kernel_maps(&session->machines.host), stderr);
> +       verbose = save_verbose;
> +}
> +
>  int cmd_script(int argc, const char **argv)
>  {
>         bool show_full_info = false;
> @@ -4366,6 +4376,9 @@ int cmd_script(int argc, const char **argv)
>
>         flush_scripting();
>
> +       if (verbose > 2)
> +               dump_kmaps(session);
> +
>  out_delete:
>         if (script.ptime_range) {
>                 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
> --
> 2.34.1
>
  
Adrian Hunter Feb. 5, 2024, 7:08 a.m. UTC | #2
On 3/02/24 03:56, Arnaldo Carvalho de Melo wrote:
> 
> 
> On Fri, Feb 2, 2024, 10:50 PM Namhyung Kim <namhyung@kernel.org <mailto:namhyung@kernel.org>> wrote:
> 
>     On Fri, Feb 2, 2024 at 3:01 AM Adrian Hunter <adrian.hunter@intel.com <mailto:adrian.hunter@intel.com>> wrote:
>     >
>     > Dump kmaps if verbose > 2.
> 
>     Maybe we can add '--debug kmap' option rather than using an
>     arbitrary verbose level.

That is a global option but would only work for tools that are
explicitly programmed to do the dump.  Could just do perf script
and perf report?

> 
> 
> I think we have 'perf report --mmap', no?

Only shows user space maps.  Could add 'perf report --kmaps'?

> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/perf/Documentation/perf-report.txt#n542 <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/perf/Documentation/perf-report.txt#n542>
> 
> - Arnaldo
> 
> Sent from smartphone
> 
> 
>     Thanks,
>     Namhyung
> 
>     >
>     > Example:
>     >
>     >   $ perf script -vvv 2>&1 >/dev/null | grep kvm.intel
>     >   build id event received for /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko: 0691d75e10e72ebbbd45a44c59f6d00a5604badf [20]
>     >   Map: 0-3a3 4f5d8 [kvm_intel].modinfo
>     >   Map: 0-5240 5f280 [kvm_intel]__versions
>     >   Map: 0-30 64 [kvm_intel].note.Linux
>     >   Map: 0-14 644c0 [kvm_intel].orc_header
>     >   Map: 0-5297 43680 [kvm_intel].rodata
>     >   Map: 0-5bee 3b837 [kvm_intel].text.unlikely
>     >   Map: 0-7e0 41430 [kvm_intel].noinstr.text
>     >   Map: 0-2080 713c0 [kvm_intel].bss
>     >   Map: 0-26 705c8 [kvm_intel].data..read_mostly
>     >   Map: 0-5888 6a4c0 [kvm_intel].data
>     >   Map: 0-22 70220 [kvm_intel].data.once
>     >   Map: 0-40 705f0 [kvm_intel].data..percpu
>     >   Map: 0-1685 41d20 [kvm_intel].init.text
>     >   Map: 0-4b8 6fd60 [kvm_intel].init.data
>     >   Map: 0-380 70248 [kvm_intel]__dyndbg
>     >   Map: 0-8 70218 [kvm_intel].exit.data
>     >   Map: 0-438 4f980 [kvm_intel]__param
>     >   Map: 0-5f5 4ca0f [kvm_intel].rodata.str1.1
>     >   Map: 0-3657 493b8 [kvm_intel].rodata.str1.8
>     >   Map: 0-e0 70640 [kvm_intel].data..ro_after_init
>     >   Map: 0-500 70ec0 [kvm_intel].gnu.linkonce.this_module
>     >   Map: ffffffffc13a7000-ffffffffc1421000 a0 /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko
>     >
>     > The example above shows how the module section mappings are all wrong
>     > except for the main .text mapping at 0xffffffffc13a7000.
>     >
>     > Signed-off-by: Adrian Hunter <adrian.hunter@intel.com <mailto:adrian.hunter@intel.com>>
>     > ---
>     >  tools/perf/builtin-script.c | 13 +++++++++++++
>     >  1 file changed, 13 insertions(+)
>     >
>     > diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
>     > index b1f57401ff23..e764b319ef59 100644
>     > --- a/tools/perf/builtin-script.c
>     > +++ b/tools/perf/builtin-script.c
>     > @@ -3806,6 +3806,16 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
>     >         return 0;
>     >  }
>     >
>     > +static void dump_kmaps(struct perf_session *session)
>     > +{
>     > +       int save_verbose = verbose;
>     > +
>     > +       pr_debug("Kernel and module maps:\n");
>     > +       verbose = 0; /* Suppress verbose to print a summary only */
>     > +       maps__fprintf(machine__kernel_maps(&session->machines.host), stderr);
>     > +       verbose = save_verbose;
>     > +}
>     > +
>     >  int cmd_script(int argc, const char **argv)
>     >  {
>     >         bool show_full_info = false;
>     > @@ -4366,6 +4376,9 @@ int cmd_script(int argc, const char **argv)
>     >
>     >         flush_scripting();
>     >
>     > +       if (verbose > 2)
>     > +               dump_kmaps(session);
>     > +
>     >  out_delete:
>     >         if (script.ptime_range) {
>     >                 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
>     > --
>     > 2.34.1
>     >
>
  
Namhyung Kim Feb. 6, 2024, 2:21 a.m. UTC | #3
On Sun, Feb 4, 2024 at 11:08 PM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> On 3/02/24 03:56, Arnaldo Carvalho de Melo wrote:
> >
> >
> > On Fri, Feb 2, 2024, 10:50 PM Namhyung Kim <namhyung@kernel.org <mailto:namhyung@kernel.org>> wrote:
> >
> >     On Fri, Feb 2, 2024 at 3:01 AM Adrian Hunter <adrian.hunter@intel.com <mailto:adrian.hunter@intel.com>> wrote:
> >     >
> >     > Dump kmaps if verbose > 2.
> >
> >     Maybe we can add '--debug kmap' option rather than using an
> >     arbitrary verbose level.
>
> That is a global option but would only work for tools that are
> explicitly programmed to do the dump.  Could just do perf script
> and perf report?

I don't care.. actually `--debug perf-event-open` would work with
commands that call the syscall only.  But I'm fine either way.

>
> >
> >
> > I think we have 'perf report --mmap', no?
>
> Only shows user space maps.  Could add 'perf report --kmaps'?

That'd work too.  It's up to you.

Thanks,
Namhyung


>
> >
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/perf/Documentation/perf-report.txt#n542 <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/perf/Documentation/perf-report.txt#n542>
> >
> > - Arnaldo
> >
> > Sent from smartphone
> >
> >
> >     Thanks,
> >     Namhyung
> >
> >     >
> >     > Example:
> >     >
> >     >   $ perf script -vvv 2>&1 >/dev/null | grep kvm.intel
> >     >   build id event received for /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko: 0691d75e10e72ebbbd45a44c59f6d00a5604badf [20]
> >     >   Map: 0-3a3 4f5d8 [kvm_intel].modinfo
> >     >   Map: 0-5240 5f280 [kvm_intel]__versions
> >     >   Map: 0-30 64 [kvm_intel].note.Linux
> >     >   Map: 0-14 644c0 [kvm_intel].orc_header
> >     >   Map: 0-5297 43680 [kvm_intel].rodata
> >     >   Map: 0-5bee 3b837 [kvm_intel].text.unlikely
> >     >   Map: 0-7e0 41430 [kvm_intel].noinstr.text
> >     >   Map: 0-2080 713c0 [kvm_intel].bss
> >     >   Map: 0-26 705c8 [kvm_intel].data..read_mostly
> >     >   Map: 0-5888 6a4c0 [kvm_intel].data
> >     >   Map: 0-22 70220 [kvm_intel].data.once
> >     >   Map: 0-40 705f0 [kvm_intel].data..percpu
> >     >   Map: 0-1685 41d20 [kvm_intel].init.text
> >     >   Map: 0-4b8 6fd60 [kvm_intel].init.data
> >     >   Map: 0-380 70248 [kvm_intel]__dyndbg
> >     >   Map: 0-8 70218 [kvm_intel].exit.data
> >     >   Map: 0-438 4f980 [kvm_intel]__param
> >     >   Map: 0-5f5 4ca0f [kvm_intel].rodata.str1.1
> >     >   Map: 0-3657 493b8 [kvm_intel].rodata.str1.8
> >     >   Map: 0-e0 70640 [kvm_intel].data..ro_after_init
> >     >   Map: 0-500 70ec0 [kvm_intel].gnu.linkonce.this_module
> >     >   Map: ffffffffc13a7000-ffffffffc1421000 a0 /lib/modules/6.7.2-local/kernel/arch/x86/kvm/kvm-intel.ko
> >     >
> >     > The example above shows how the module section mappings are all wrong
> >     > except for the main .text mapping at 0xffffffffc13a7000.
> >     >
> >     > Signed-off-by: Adrian Hunter <adrian.hunter@intel.com <mailto:adrian.hunter@intel.com>>
> >     > ---
> >     >  tools/perf/builtin-script.c | 13 +++++++++++++
> >     >  1 file changed, 13 insertions(+)
> >     >
> >     > diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> >     > index b1f57401ff23..e764b319ef59 100644
> >     > --- a/tools/perf/builtin-script.c
> >     > +++ b/tools/perf/builtin-script.c
> >     > @@ -3806,6 +3806,16 @@ static int parse_callret_trace(const struct option *opt __maybe_unused,
> >     >         return 0;
> >     >  }
> >     >
> >     > +static void dump_kmaps(struct perf_session *session)
> >     > +{
> >     > +       int save_verbose = verbose;
> >     > +
> >     > +       pr_debug("Kernel and module maps:\n");
> >     > +       verbose = 0; /* Suppress verbose to print a summary only */
> >     > +       maps__fprintf(machine__kernel_maps(&session->machines.host), stderr);
> >     > +       verbose = save_verbose;
> >     > +}
> >     > +
> >     >  int cmd_script(int argc, const char **argv)
> >     >  {
> >     >         bool show_full_info = false;
> >     > @@ -4366,6 +4376,9 @@ int cmd_script(int argc, const char **argv)
> >     >
> >     >         flush_scripting();
> >     >
> >     > +       if (verbose > 2)
> >     > +               dump_kmaps(session);
> >     > +
> >     >  out_delete:
> >     >         if (script.ptime_range) {
> >     >                 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
> >     > --
> >     > 2.34.1
> >     >
> >
>
  

Patch

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index b1f57401ff23..e764b319ef59 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -3806,6 +3806,16 @@  static int parse_callret_trace(const struct option *opt __maybe_unused,
 	return 0;
 }
 
+static void dump_kmaps(struct perf_session *session)
+{
+	int save_verbose = verbose;
+
+	pr_debug("Kernel and module maps:\n");
+	verbose = 0; /* Suppress verbose to print a summary only */
+	maps__fprintf(machine__kernel_maps(&session->machines.host), stderr);
+	verbose = save_verbose;
+}
+
 int cmd_script(int argc, const char **argv)
 {
 	bool show_full_info = false;
@@ -4366,6 +4376,9 @@  int cmd_script(int argc, const char **argv)
 
 	flush_scripting();
 
+	if (verbose > 2)
+		dump_kmaps(session);
+
 out_delete:
 	if (script.ptime_range) {
 		itrace_synth_opts__clear_time_range(&itrace_synth_opts);