Skip to content

Commit

Permalink
perf_counter tools: Connect module support infrastructure to symbol l…
Browse files Browse the repository at this point in the history
…oading infrastructure

Signed-off-by: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
LKML-Reference: <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Mike Galbraith authored and Ingo Molnar committed Jul 2, 2009
1 parent 208b4b4 commit 6cfcc53
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tools/perf/builtin-annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static int load_kernel(void)
if (!kernel_dso)
return -1;

err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, 0);
if (err <= 0) {
dso__delete(kernel_dso);
kernel_dso = NULL;
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static int load_kernel(void)
if (!kernel_dso)
return -1;

err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, 0);
if (err <= 0) {
dso__delete(kernel_dso);
kernel_dso = NULL;
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-top.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ static int parse_symbols(void)
if (kernel_dso == NULL)
return -1;

if (dso__load_kernel(kernel_dso, NULL, symbol_filter, 1) <= 0)
if (dso__load_kernel(kernel_dso, NULL, symbol_filter, 1, 0) <= 0)
goto out_delete_dso;

node = rb_first(&kernel_dso->syms);
Expand Down
159 changes: 149 additions & 10 deletions tools/perf/util/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static struct symbol *symbol__new(u64 start, u64 len,
self = ((void *)self) + priv_size;
}
self->start = start;
self->end = start + len - 1;
self->end = len ? start + len - 1 : start;
memcpy(self->name, name, namelen);

return self;
Expand All @@ -48,8 +48,12 @@ static void symbol__delete(struct symbol *self, unsigned int priv_size)

static size_t symbol__fprintf(struct symbol *self, FILE *fp)
{
return fprintf(fp, " %llx-%llx %s\n",
if (!self->module)
return fprintf(fp, " %llx-%llx %s\n",
self->start, self->end, self->name);
else
return fprintf(fp, " %llx-%llx %s \t[%s]\n",
self->start, self->end, self->name, self->module->name);
}

struct dso *dso__new(const char *name, unsigned int sym_priv_size)
Expand Down Expand Up @@ -310,6 +314,26 @@ static inline int elf_sym__is_function(const GElf_Sym *sym)
sym->st_size != 0;
}

static inline int elf_sym__is_label(const GElf_Sym *sym)
{
return elf_sym__type(sym) == STT_NOTYPE &&
sym->st_name != 0 &&
sym->st_shndx != SHN_UNDEF &&
sym->st_shndx != SHN_ABS;
}

static inline const char *elf_sec__name(const GElf_Shdr *shdr,
const Elf_Data *secstrs)
{
return secstrs->d_buf + shdr->sh_name;
}

static inline int elf_sec__is_text(const GElf_Shdr *shdr,
const Elf_Data *secstrs)
{
return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
}

static inline const char *elf_sym__name(const GElf_Sym *sym,
const Elf_Data *symstrs)
{
Expand Down Expand Up @@ -451,17 +475,17 @@ static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf,
}

static int dso__load_sym(struct dso *self, int fd, const char *name,
symbol_filter_t filter, int verbose)
symbol_filter_t filter, int verbose, struct module *mod)
{
Elf_Data *symstrs;
Elf_Data *symstrs, *secstrs;
uint32_t nr_syms;
int err = -1;
uint32_t index;
GElf_Ehdr ehdr;
GElf_Shdr shdr;
Elf_Data *syms;
GElf_Sym sym;
Elf_Scn *sec, *sec_dynsym;
Elf_Scn *sec, *sec_dynsym, *sec_strndx;
Elf *elf;
size_t dynsym_idx;
int nr = 0;
Expand Down Expand Up @@ -520,6 +544,14 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
if (symstrs == NULL)
goto out_elf_end;

sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
if (sec_strndx == NULL)
goto out_elf_end;

secstrs = elf_getdata(sec_strndx, NULL);
if (symstrs == NULL)
goto out_elf_end;

nr_syms = shdr.sh_size / shdr.sh_entsize;

memset(&sym, 0, sizeof(sym));
Expand All @@ -529,15 +561,23 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
struct symbol *f;
u64 obj_start;
struct section *section = NULL;
int is_label = elf_sym__is_label(&sym);
const char *section_name;

if (!elf_sym__is_function(&sym))
if (!is_label && !elf_sym__is_function(&sym))
continue;

sec = elf_getscn(elf, sym.st_shndx);
if (!sec)
goto out_elf_end;

gelf_getshdr(sec, &shdr);

if (is_label && !elf_sec__is_text(&shdr, secstrs))
continue;

section_name = elf_sec__name(&shdr, secstrs);
obj_start = sym.st_value;

if (self->prelinked) {
Expand All @@ -548,6 +588,17 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
sym.st_value -= shdr.sh_addr - shdr.sh_offset;
}

if (mod) {
section = mod->sections->find_section(mod->sections, section_name);
if (section)
sym.st_value += section->vma;
else {
fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
mod->name, section_name);
goto out_elf_end;
}
}

f = symbol__new(sym.st_value, sym.st_size,
elf_sym__name(&sym, symstrs),
self->sym_priv_size, obj_start, verbose);
Expand All @@ -557,6 +608,7 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
if (filter && filter(self, f))
symbol__delete(f, self->sym_priv_size);
else {
f->module = mod;
dso__insert_symbol(self, f);
nr++;
}
Expand Down Expand Up @@ -606,7 +658,7 @@ int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
fd = open(name, O_RDONLY);
} while (fd < 0);

ret = dso__load_sym(self, fd, name, filter, verbose);
ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
close(fd);

/*
Expand All @@ -620,6 +672,86 @@ int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
return ret;
}

static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
symbol_filter_t filter, int verbose)
{
struct module *mod = mod_dso__find_module(mods, name);
int err = 0, fd;

if (mod == NULL || !mod->active)
return err;

fd = open(mod->path, O_RDONLY);

if (fd < 0)
return err;

err = dso__load_sym(self, fd, name, filter, verbose, mod);
close(fd);

return err;
}

int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
{
struct mod_dso *mods = mod_dso__new_dso("modules");
struct module *pos;
struct rb_node *next;
int err;

err = mod_dso__load_modules(mods);

if (err <= 0)
return err;

/*
* Iterate over modules, and load active symbols.
*/
next = rb_first(&mods->mods);
while (next) {
pos = rb_entry(next, struct module, rb_node);
err = dso__load_module(self, mods, pos->name, filter, verbose);

if (err < 0)
break;

next = rb_next(&pos->rb_node);
}

if (err < 0) {
mod_dso__delete_modules(mods);
mod_dso__delete_self(mods);
}

return err;
}

static inline void dso__fill_symbol_holes(struct dso *self)
{
struct symbol *prev = NULL;
struct rb_node *nd;

for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
struct symbol *pos = rb_entry(nd, struct symbol, rb_node);

if (prev) {
u64 hole = 0;
int alias = pos->start == prev->start;

if (!alias)
hole = prev->start - pos->end - 1;

if (hole || alias) {
if (alias)
pos->end = prev->end;
else if (hole)
pos->end = prev->start - 1;
}
}
prev = pos;
}
}

static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
symbol_filter_t filter, int verbose)
{
Expand All @@ -628,19 +760,26 @@ static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
if (fd < 0)
return -1;

err = dso__load_sym(self, fd, vmlinux, filter, verbose);
err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);

if (err > 0)
dso__fill_symbol_holes(self);

close(fd);

return err;
}

int dso__load_kernel(struct dso *self, const char *vmlinux,
symbol_filter_t filter, int verbose)
symbol_filter_t filter, int verbose, int modules)
{
int err = -1;

if (vmlinux)
if (vmlinux) {
err = dso__load_vmlinux(self, vmlinux, filter, verbose);
if (err > 0 && modules)
err = dso__load_modules(self, filter, verbose);
}

if (err <= 0)
err = dso__load_kallsyms(self, filter, verbose);
Expand Down
5 changes: 4 additions & 1 deletion tools/perf/util/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "types.h"
#include <linux/list.h>
#include <linux/rbtree.h>
#include "module.h"

struct symbol {
struct rb_node rb_node;
Expand All @@ -13,6 +14,7 @@ struct symbol {
u64 obj_start;
u64 hist_sum;
u64 *hist;
struct module *module;
void *priv;
char name[0];
};
Expand Down Expand Up @@ -41,7 +43,8 @@ static inline void *dso__sym_priv(struct dso *self, struct symbol *sym)
struct symbol *dso__find_symbol(struct dso *self, u64 ip);

int dso__load_kernel(struct dso *self, const char *vmlinux,
symbol_filter_t filter, int verbose);
symbol_filter_t filter, int verbose, int modules);
int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose);
int dso__load(struct dso *self, symbol_filter_t filter, int verbose);

size_t dso__fprintf(struct dso *self, FILE *fp);
Expand Down

0 comments on commit 6cfcc53

Please sign in to comment.