Re: [RFC bpf-next 1/4] kallsyms: Add kallsyms_lookup_names function
From: Jiri Olsa
Date: Wed Apr 13 2022 - 03:27:51 EST
On Fri, Apr 08, 2022 at 09:57:01AM +0900, Masami Hiramatsu wrote:
SNIP
> > /*
> > * These will be re-linked against their real values
> > @@ -572,6 +574,52 @@ int sprint_backtrace_build_id(char *buffer, unsigned long address)
> > return __sprint_symbol(buffer, address, -1, 1, 1);
> > }
> >
> > +static int symbols_cmp(const void *a, const void *b)
> > +{
> > + const char **str_a = (const char **) a;
> > + const char **str_b = (const char **) b;
> > +
> > + return strcmp(*str_a, *str_b);
> > +}
> > +
> > +struct kallsyms_data {
> > + unsigned long *addrs;
> > + const char **syms;
> > + u32 cnt;
> > + u32 found;
>
> BTW, why do you use 'u32' for this arch independent code?
> I think 'size_t' will make its role clearer.
right, will change
>
> > +};
> > +
> > +static int kallsyms_callback(void *data, const char *name,
> > + struct module *mod, unsigned long addr)
> > +{
> > + struct kallsyms_data *args = data;
> > +
> > + if (!bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp))
> > + return 0;
> > +
> > + addr = ftrace_location(addr);
> > + if (!addr)
> > + return 0;
> > +
> > + args->addrs[args->found++] = addr;
> > + return args->found == args->cnt ? 1 : 0;
> > +}
> > +
> > +int kallsyms_lookup_names(const char **syms, u32 cnt, unsigned long *addrs)
>
> Ditto. I think 'size_t cnt' is better.
ok, thanks
jirka
>
> Thank you,
>
> > +{
> > + struct kallsyms_data args;
> > +
> > + sort(syms, cnt, sizeof(*syms), symbols_cmp, NULL);
> > +
> > + args.addrs = addrs;
> > + args.syms = syms;
> > + args.cnt = cnt;
> > + args.found = 0;
> > + kallsyms_on_each_symbol(kallsyms_callback, &args);
> > +
> > + return args.found == args.cnt ? 0 : -EINVAL;
> > +}
> > +
> > /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
> > struct kallsym_iter {
> > loff_t pos;
> > --
> > 2.35.1
> >
>
>
> --
> Masami Hiramatsu <mhiramat@xxxxxxxxxx>