2023-07-25 00:46:13

by Namhyung Kim

[permalink] [raw]
Subject: [PATCH v2 1/2] perf tools: Add kallsyms__get_symbol_start()

The kallsyms__get_symbol_start() to get any symbol address from
kallsyms. The existing kallsyms__get_function_start() only allows text
symbols so create this to allow data symbols too.

Acked-by: Ian Rogers <[email protected]>
Signed-off-by: Namhyung Kim <[email protected]>
---
tools/perf/util/event.c | 30 +++++++++++++++++++++++++++---
tools/perf/util/event.h | 2 ++
2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 4cbb092e0684..923c0fb15122 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -93,8 +93,8 @@ struct process_symbol_args {
u64 start;
};

-static int find_symbol_cb(void *arg, const char *name, char type,
- u64 start)
+static int find_func_symbol_cb(void *arg, const char *name, char type,
+ u64 start)
{
struct process_symbol_args *args = arg;

@@ -110,12 +110,36 @@ static int find_symbol_cb(void *arg, const char *name, char type,
return 1;
}

+static int find_any_symbol_cb(void *arg, const char *name,
+ char type __maybe_unused, u64 start)
+{
+ struct process_symbol_args *args = arg;
+
+ if (strcmp(name, args->name))
+ return 0;
+
+ args->start = start;
+ return 1;
+}
+
int kallsyms__get_function_start(const char *kallsyms_filename,
const char *symbol_name, u64 *addr)
{
struct process_symbol_args args = { .name = symbol_name, };

- if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
+ if (kallsyms__parse(kallsyms_filename, &args, find_func_symbol_cb) <= 0)
+ return -1;
+
+ *addr = args.start;
+ return 0;
+}
+
+int kallsyms__get_symbol_start(const char *kallsyms_filename,
+ const char *symbol_name, u64 *addr)
+{
+ struct process_symbol_args args = { .name = symbol_name, };
+
+ if (kallsyms__parse(kallsyms_filename, &args, find_any_symbol_cb) <= 0)
return -1;

*addr = args.start;
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index de20e01c9d72..d8bcee2e9b93 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -360,6 +360,8 @@ size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FIL

int kallsyms__get_function_start(const char *kallsyms_filename,
const char *symbol_name, u64 *addr);
+int kallsyms__get_symbol_start(const char *kallsyms_filename,
+ const char *symbol_name, u64 *addr);

void event_attr_init(struct perf_event_attr *attr);

--
2.41.0.487.g6d72f3e995-goog



2023-07-25 14:25:11

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] perf tools: Add kallsyms__get_symbol_start()

On 25/07/23 03:19, Namhyung Kim wrote:
> The kallsyms__get_symbol_start() to get any symbol address from
> kallsyms. The existing kallsyms__get_function_start() only allows text
> symbols so create this to allow data symbols too.
>
> Acked-by: Ian Rogers <[email protected]>
> Signed-off-by: Namhyung Kim <[email protected]>

Acked-by: Adrian Hunter <[email protected]>

> ---
> tools/perf/util/event.c | 30 +++++++++++++++++++++++++++---
> tools/perf/util/event.h | 2 ++
> 2 files changed, 29 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
> index 4cbb092e0684..923c0fb15122 100644
> --- a/tools/perf/util/event.c
> +++ b/tools/perf/util/event.c
> @@ -93,8 +93,8 @@ struct process_symbol_args {
> u64 start;
> };
>
> -static int find_symbol_cb(void *arg, const char *name, char type,
> - u64 start)
> +static int find_func_symbol_cb(void *arg, const char *name, char type,
> + u64 start)
> {
> struct process_symbol_args *args = arg;
>
> @@ -110,12 +110,36 @@ static int find_symbol_cb(void *arg, const char *name, char type,
> return 1;
> }
>
> +static int find_any_symbol_cb(void *arg, const char *name,
> + char type __maybe_unused, u64 start)
> +{
> + struct process_symbol_args *args = arg;
> +
> + if (strcmp(name, args->name))
> + return 0;
> +
> + args->start = start;
> + return 1;
> +}
> +
> int kallsyms__get_function_start(const char *kallsyms_filename,
> const char *symbol_name, u64 *addr)
> {
> struct process_symbol_args args = { .name = symbol_name, };
>
> - if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
> + if (kallsyms__parse(kallsyms_filename, &args, find_func_symbol_cb) <= 0)
> + return -1;
> +
> + *addr = args.start;
> + return 0;
> +}
> +
> +int kallsyms__get_symbol_start(const char *kallsyms_filename,
> + const char *symbol_name, u64 *addr)
> +{
> + struct process_symbol_args args = { .name = symbol_name, };
> +
> + if (kallsyms__parse(kallsyms_filename, &args, find_any_symbol_cb) <= 0)
> return -1;
>
> *addr = args.start;
> diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
> index de20e01c9d72..d8bcee2e9b93 100644
> --- a/tools/perf/util/event.h
> +++ b/tools/perf/util/event.h
> @@ -360,6 +360,8 @@ size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FIL
>
> int kallsyms__get_function_start(const char *kallsyms_filename,
> const char *symbol_name, u64 *addr);
> +int kallsyms__get_symbol_start(const char *kallsyms_filename,
> + const char *symbol_name, u64 *addr);
>
> void event_attr_init(struct perf_event_attr *attr);
>


2023-07-26 20:52:25

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] perf tools: Add kallsyms__get_symbol_start()

Em Tue, Jul 25, 2023 at 05:07:40PM +0300, Adrian Hunter escreveu:
> On 25/07/23 03:19, Namhyung Kim wrote:
> > The kallsyms__get_symbol_start() to get any symbol address from
> > kallsyms. The existing kallsyms__get_function_start() only allows text
> > symbols so create this to allow data symbols too.
> >
> > Acked-by: Ian Rogers <[email protected]>
> > Signed-off-by: Namhyung Kim <[email protected]>
>
> Acked-by: Adrian Hunter <[email protected]>

Thanks, applied both patches.

- Arnaldo

> > ---
> > tools/perf/util/event.c | 30 +++++++++++++++++++++++++++---
> > tools/perf/util/event.h | 2 ++
> > 2 files changed, 29 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
> > index 4cbb092e0684..923c0fb15122 100644
> > --- a/tools/perf/util/event.c
> > +++ b/tools/perf/util/event.c
> > @@ -93,8 +93,8 @@ struct process_symbol_args {
> > u64 start;
> > };
> >
> > -static int find_symbol_cb(void *arg, const char *name, char type,
> > - u64 start)
> > +static int find_func_symbol_cb(void *arg, const char *name, char type,
> > + u64 start)
> > {
> > struct process_symbol_args *args = arg;
> >
> > @@ -110,12 +110,36 @@ static int find_symbol_cb(void *arg, const char *name, char type,
> > return 1;
> > }
> >
> > +static int find_any_symbol_cb(void *arg, const char *name,
> > + char type __maybe_unused, u64 start)
> > +{
> > + struct process_symbol_args *args = arg;
> > +
> > + if (strcmp(name, args->name))
> > + return 0;
> > +
> > + args->start = start;
> > + return 1;
> > +}
> > +
> > int kallsyms__get_function_start(const char *kallsyms_filename,
> > const char *symbol_name, u64 *addr)
> > {
> > struct process_symbol_args args = { .name = symbol_name, };
> >
> > - if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
> > + if (kallsyms__parse(kallsyms_filename, &args, find_func_symbol_cb) <= 0)
> > + return -1;
> > +
> > + *addr = args.start;
> > + return 0;
> > +}
> > +
> > +int kallsyms__get_symbol_start(const char *kallsyms_filename,
> > + const char *symbol_name, u64 *addr)
> > +{
> > + struct process_symbol_args args = { .name = symbol_name, };
> > +
> > + if (kallsyms__parse(kallsyms_filename, &args, find_any_symbol_cb) <= 0)
> > return -1;
> >
> > *addr = args.start;
> > diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
> > index de20e01c9d72..d8bcee2e9b93 100644
> > --- a/tools/perf/util/event.h
> > +++ b/tools/perf/util/event.h
> > @@ -360,6 +360,8 @@ size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FIL
> >
> > int kallsyms__get_function_start(const char *kallsyms_filename,
> > const char *symbol_name, u64 *addr);
> > +int kallsyms__get_symbol_start(const char *kallsyms_filename,
> > + const char *symbol_name, u64 *addr);
> >
> > void event_attr_init(struct perf_event_attr *attr);
> >
>

--

- Arnaldo