On Tue, Apr 27, 2021 at 01:42:40PM +0200, Jiri Olsa wrote: > On Fri, Apr 23, 2021 at 02:37:28PM -0700, Martin KaFai Lau wrote: > > SNIP > > > +static int collect_listed_functions(struct btf_elf *btfe, GElf_Sym *sym, > > + size_t sym_sec_idx) > > +{ > > + int len, digits = 0, underscores = 0; > > + const char *name; > > + char *func_name; > > + > > + if (!btfe->btf_ids_shndx || > > + btfe->btf_ids_shndx != sym_sec_idx) > > + return 0; > > + > > + /* The kernel function in the btf id list will have symbol like: > > + * __BTF_ID__func__<kernel_func_name>__[digit]+ > > + */ > > + name = elf_sym__name(sym, btfe->symtab); > > + if (strncmp(name, BTF_ID_FUNC_PREFIX, BTF_ID_FUNC_PREFIX_LEN)) > > + return 0; > > + > > + name += BTF_ID_FUNC_PREFIX_LEN; > > + > > + /* name: <kernel_func_name>__[digit]+ > > + * Strip the ending __[digit]+ > > + */ > > + for (len = strlen(name); len && underscores != 2; len--) { > > + char c = name[len - 1]; > > + > > + if (c == '_') { > > + if (!digits) > > + return 0; > > + underscores++; > > + } else if (isdigit(c)) { > > + if (underscores) > > + return 0; > > + digits++; > > + } else { > > + return 0; > > + } > > + } > > + > > + if (!len) > > + return 0; > > + > > + func_name = strndup(name, len); > > + if (!func_name) { > > + fprintf(stderr, > > + "Failed to alloc memory for listed function %s%s\n", > > + BTF_ID_FUNC_PREFIX, name); > > + return -1; > > + } > > + > > + if (is_listed_func(func_name)) { > > + /* already captured */ > > + free(func_name); > > + return 0; > > + } > > + > > + /* grow listed_functions */ > > + if (listed_functions_cnt == listed_functions_alloc) { > > + char **new; > > + > > + listed_functions_alloc = max(100, > > + listed_functions_alloc * 3 / 2); > > + new = realloc(listed_functions, > > + listed_functions_alloc * sizeof(*listed_functions)); > > + if (!new) { > > + fprintf(stderr, > > + "Failed to alloc memory for listed function %s%s\n", > > + BTF_ID_FUNC_PREFIX, name); > > + free(func_name); > > + return -1; > > + } > > + listed_functions = new; > > + } > > + > > + listed_functions[listed_functions_cnt++] = func_name; > > + qsort(listed_functions, listed_functions_cnt, > > + sizeof(*listed_functions), listed_function_cmp); > > I was thinking of doing this at the end in setup_functions, > but we need to do name lookups before adding.. I am planning to just use btf__add_str() as Andrii suggested. Then most of these will go away.