Skip to content

Commit

Permalink
perf map: Add accessor for dso
Browse files Browse the repository at this point in the history
Later changes will add reference count checking for struct map, with
dso being the most frequently accessed variable. Add an accessor so
that the reference count check is only necessary in one place.

Additional changes:
 - add a dso variable to avoid repeated map__dso calls.
 - in builtin-mem.c dump_raw_samples, code only partially tested for
   dso == NULL. Make the possibility of NULL consistent.
 - in thread.c thread__memcpy fix use of spaces and use tabs.

Committer notes:

Did missing conversions on these files:

   tools/perf/arch/powerpc/util/skip-callchain-idx.c
   tools/perf/arch/powerpc/util/sym-handling.c
   tools/perf/ui/browsers/hists.c
   tools/perf/ui/gtk/annotate.c
   tools/perf/util/cs-etm.c
   tools/perf/util/thread.c
   tools/perf/util/unwind-libunwind-local.c
   tools/perf/util/unwind-libunwind.c

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miaoqian Lin <linmq006@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Yury Norov <yury.norov@gmail.com>
Link: https://lore.kernel.org/r/20230320212248.1175731-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Ian Rogers authored and Arnaldo Carvalho de Melo committed Apr 4, 2023
1 parent 5ab6d71 commit 63df0e4
Show file tree
Hide file tree
Showing 54 changed files with 447 additions and 319 deletions.
2 changes: 1 addition & 1 deletion tools/perf/arch/powerpc/util/skip-callchain-idx.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);

if (al.map)
dso = al.map->dso;
dso = map__dso(al.map);

if (!dso) {
pr_debug("%" PRIx64 " dso is NULL\n", ip);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/arch/powerpc/util/sym-handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void arch__fix_tev_from_maps(struct perf_probe_event *pev,

lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);

if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
if (map__dso(map)->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
tev->point.offset += PPC64LE_LEP_OFFSET;
else if (lep_offset) {
if (pev->uprobes)
Expand Down
11 changes: 6 additions & 5 deletions tools/perf/builtin-annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static int process_branch_callback(struct evsel *evsel,
return 0;

if (a.map != NULL)
a.map->dso->hit = 1;
map__dso(a.map)->hit = 1;

hist__account_cycles(sample->branch_stack, al, sample, false, NULL);

Expand Down Expand Up @@ -235,10 +235,11 @@ static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
* the DSO?
*/
if (al->sym != NULL) {
rb_erase_cached(&al->sym->rb_node,
&al->map->dso->symbols);
struct dso *dso = map__dso(al->map);

rb_erase_cached(&al->sym->rb_node, &dso->symbols);
symbol__delete(al->sym);
dso__reset_find_symbol_cache(al->map->dso);
dso__reset_find_symbol_cache(dso);
}
return 0;
}
Expand Down Expand Up @@ -320,7 +321,7 @@ static void hists__find_annotations(struct hists *hists,
struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
struct annotation *notes;

if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
if (he->ms.sym == NULL || map__dso(he->ms.map)->annotate_warned)
goto find_next;

if (ann->sym_hist_filter &&
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-buildid-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
{
const struct dso *dso = map->dso;
const struct dso *dso = map__dso(map);
char bid_buf[SBUILD_ID_SIZE];

memset(bid_buf, 0, sizeof(bid_buf));
Expand Down
8 changes: 5 additions & 3 deletions tools/perf/builtin-inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,11 @@ int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,
}

if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
if (!al.map->dso->hit) {
al.map->dso->hit = 1;
dso__inject_build_id(al.map->dso, tool, machine,
struct dso *dso = map__dso(al.map);

if (!dso->hit) {
dso->hit = 1;
dso__inject_build_id(dso, tool, machine,
sample->cpumode, al.map->flags);
}
}
Expand Down
4 changes: 3 additions & 1 deletion tools/perf/builtin-kallsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ static int __cmd_kallsyms(int argc, const char **argv)

for (i = 0; i < argc; ++i) {
struct map *map;
const struct dso *dso;
struct symbol *symbol = machine__find_kernel_symbol_by_name(machine, argv[i], &map);

if (symbol == NULL) {
printf("%s: not found\n", argv[i]);
continue;
}

dso = map__dso(map);
printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n",
symbol->name, map->dso->short_name, map->dso->long_name,
symbol->name, dso->short_name, dso->long_name,
map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end),
symbol->start, symbol->end);
}
Expand Down
10 changes: 7 additions & 3 deletions tools/perf/builtin-mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ dump_raw_samples(struct perf_tool *tool,
struct addr_location al;
const char *fmt, *field_sep;
char str[PAGE_SIZE_NAME_LEN];
struct dso *dso = NULL;

if (machine__resolve(machine, &al, sample) < 0) {
fprintf(stderr, "problem processing %d event, skipping it.\n",
Expand All @@ -210,8 +211,11 @@ dump_raw_samples(struct perf_tool *tool,
if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
goto out_put;

if (al.map != NULL)
al.map->dso->hit = 1;
if (al.map != NULL) {
dso = map__dso(al.map);
if (dso)
dso->hit = 1;
}

field_sep = symbol_conf.field_sep;
if (field_sep) {
Expand Down Expand Up @@ -252,7 +256,7 @@ dump_raw_samples(struct perf_tool *tool,
symbol_conf.field_sep,
sample->data_src,
symbol_conf.field_sep,
al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
dso ? dso->long_name : "???",
al.sym ? al.sym->name : "???");
out_put:
addr_location__put(&al);
Expand Down
7 changes: 4 additions & 3 deletions tools/perf/builtin-report.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static int process_sample_event(struct perf_tool *tool,
}

if (al.map != NULL)
al.map->dso->hit = 1;
map__dso(al.map)->hit = 1;

if (ui__has_annotation() || rep->symbol_ipc || rep->total_cycles_mode) {
hist__account_cycles(sample->branch_stack, &al, sample,
Expand Down Expand Up @@ -608,7 +608,7 @@ static void report__warn_kptr_restrict(const struct report *rep)
return;

if (kernel_map == NULL ||
(kernel_map->dso->hit &&
(map__dso(kernel_map)->hit &&
(kernel_kmap->ref_reloc_sym == NULL ||
kernel_kmap->ref_reloc_sym->addr == 0))) {
const char *desc =
Expand Down Expand Up @@ -848,6 +848,7 @@ static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)

maps__for_each_entry(maps, rb_node) {
struct map *map = rb_node->map;
const struct dso *dso = map__dso(map);

printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
indent, "", map->start, map->end,
Expand All @@ -856,7 +857,7 @@ static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
map->prot & PROT_EXEC ? 'x' : '-',
map->flags & MAP_SHARED ? 's' : 'p',
map->pgoff,
map->dso->id.ino, map->dso->name);
dso->id.ino, dso->name);
}

return printed;
Expand Down
19 changes: 11 additions & 8 deletions tools/perf/builtin-script.c
Original file line number Diff line number Diff line change
Expand Up @@ -1011,11 +1011,11 @@ static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
to = entries[i].to;

if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
!alf.map->dso->adjust_symbols)
!map__dso(alf.map)->adjust_symbols)
from = map__map_ip(alf.map, from);

if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
!alt.map->dso->adjust_symbols)
!map__dso(alt.map)->adjust_symbols)
to = map__map_ip(alt.map, to);

printed += fprintf(fp, " 0x%"PRIx64, from);
Expand Down Expand Up @@ -1044,6 +1044,7 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,
long offset, len;
struct addr_location al;
bool kernel;
struct dso *dso;

if (!start || !end)
return 0;
Expand Down Expand Up @@ -1074,11 +1075,12 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,
return 0;
}

if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
dso = map__dso(al.map);
if (!thread__find_map(thread, *cpumode, start, &al) || !dso) {
pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
return 0;
}
if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
if (dso->data.status == DSO_DATA_STATUS_ERROR) {
pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
return 0;
}
Expand All @@ -1087,10 +1089,10 @@ static int grab_bb(u8 *buffer, u64 start, u64 end,
map__load(al.map);

offset = al.map->map_ip(al.map, start);
len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
len = dso__data_read_offset(dso, machine, offset, (u8 *)buffer,
end - start + MAXINSN);

*is64bit = al.map->dso->is_64_bit;
*is64bit = dso->is_64_bit;
if (len <= 0)
pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
start, end);
Expand All @@ -1104,10 +1106,11 @@ static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srcc
unsigned line;
int len;
char *srccode;
struct dso *dso = map__dso(map);

if (!map || !map->dso)
if (!map || !dso)
return 0;
srcfile = get_srcline_split(map->dso,
srcfile = get_srcline_split(dso,
map__rip_2objdump(map, addr),
&line);
if (!srcfile)
Expand Down
11 changes: 7 additions & 4 deletions tools/perf/builtin-top.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
struct symbol *sym;
struct annotation *notes;
struct map *map;
struct dso *dso;
int err = -1;

if (!he || !he->ms.sym)
Expand All @@ -123,12 +124,12 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)

sym = he->ms.sym;
map = he->ms.map;
dso = map__dso(map);

/*
* We can't annotate with just /proc/kallsyms
*/
if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
!dso__is_kcore(map->dso)) {
if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(dso)) {
pr_err("Can't annotate %s: No vmlinux file was found in the "
"path\n", sym->name);
sleep(1);
Expand Down Expand Up @@ -169,6 +170,7 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
{
struct utsname uts;
int err = uname(&uts);
struct dso *dso = map__dso(map);

ui__warning("Out of bounds address found:\n\n"
"Addr: %" PRIx64 "\n"
Expand All @@ -180,7 +182,7 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
"Tools: %s\n\n"
"Not all samples will be on the annotation output.\n\n"
"Please report to linux-kernel@vger.kernel.org\n",
ip, map->dso->long_name, dso__symtab_origin(map->dso),
ip, dso->long_name, dso__symtab_origin(dso),
map->start, map->end, sym->start, sym->end,
sym->binding == STB_GLOBAL ? 'g' :
sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
Expand Down Expand Up @@ -810,7 +812,8 @@ static void perf_event__process_sample(struct perf_tool *tool,
__map__is_kernel(al.map) && map__has_symbols(al.map)) {
if (symbol_conf.vmlinux_name) {
char serr[256];
dso__strerror_load(al.map->dso, serr, sizeof(serr));

dso__strerror_load(map__dso(al.map), serr, sizeof(serr));
ui__warning("The %s file can't be used: %s\n%s",
symbol_conf.vmlinux_name, serr, msg);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -2863,7 +2863,7 @@ static void print_location(FILE *f, struct perf_sample *sample,
{

if ((verbose > 0 || print_dso) && al->map)
fprintf(f, "%s@", al->map->dso->long_name);
fprintf(f, "%s@", map__dso(al->map)->long_name);

if ((verbose > 0 || print_sym) && al->sym)
fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Expand Down
6 changes: 4 additions & 2 deletions tools/perf/scripts/python/Perf-Trace-Util/Context.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode
char *srccode = NULL;
PyObject *result;
struct map *map;
struct dso *dso;
int len = 0;
u64 addr;

Expand All @@ -153,9 +154,10 @@ static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode

map = c->al->map;
addr = c->al->addr;
dso = map ? map__dso(map) : NULL;

if (map && map->dso)
srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line);
if (dso)
srcfile = get_srcline_split(dso, map__rip_2objdump(map, addr), &line);

if (get_srccode) {
if (srcfile)
Expand Down
28 changes: 15 additions & 13 deletions tools/perf/tests/code-reading.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,11 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
char decomp_name[KMOD_DECOMP_LEN];
bool decomp = false;
int ret, err = 0;
struct dso *dso;

pr_debug("Reading object code for memory address: %#"PRIx64"\n", addr);

if (!thread__find_map(thread, cpumode, addr, &al) || !al.map->dso) {
if (!thread__find_map(thread, cpumode, addr, &al) || !map__dso(al.map)) {
if (cpumode == PERF_RECORD_MISC_HYPERVISOR) {
pr_debug("Hypervisor address can not be resolved - skipping\n");
goto out;
Expand All @@ -250,11 +251,10 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
err = -1;
goto out;
}
dso = map__dso(al.map);
pr_debug("File is: %s\n", dso->long_name);

pr_debug("File is: %s\n", al.map->dso->long_name);

if (al.map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
!dso__is_kcore(al.map->dso)) {
if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(dso)) {
pr_debug("Unexpected kernel address - skipping\n");
goto out;
}
Expand All @@ -269,7 +269,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
len = al.map->end - addr;

/* Read the object code using perf */
ret_len = dso__data_read_offset(al.map->dso, maps__machine(thread->maps),
ret_len = dso__data_read_offset(dso, maps__machine(thread->maps),
al.addr, buf1, len);
if (ret_len != len) {
pr_debug("dso__data_read_offset failed\n");
Expand All @@ -287,7 +287,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
}

/* objdump struggles with kcore - try each map only once */
if (dso__is_kcore(al.map->dso)) {
if (dso__is_kcore(dso)) {
size_t d;

for (d = 0; d < state->done_cnt; d++) {
Expand All @@ -304,9 +304,9 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
state->done[state->done_cnt++] = al.map->start;
}

objdump_name = al.map->dso->long_name;
if (dso__needs_decompress(al.map->dso)) {
if (dso__decompress_kmodule_path(al.map->dso, objdump_name,
objdump_name = dso->long_name;
if (dso__needs_decompress(dso)) {
if (dso__decompress_kmodule_path(dso, objdump_name,
decomp_name,
sizeof(decomp_name)) < 0) {
pr_debug("decompression failed\n");
Expand Down Expand Up @@ -335,7 +335,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
len -= ret;
if (len) {
pr_debug("Reducing len to %zu\n", len);
} else if (dso__is_kcore(al.map->dso)) {
} else if (dso__is_kcore(dso)) {
/*
* objdump cannot handle very large segments
* that may be found in kcore.
Expand Down Expand Up @@ -572,6 +572,7 @@ static int do_test_code_reading(bool try_kcore)
pid_t pid;
struct map *map;
bool have_vmlinux, have_kcore, excl_kernel = false;
struct dso *dso;

pid = getpid();

Expand All @@ -595,8 +596,9 @@ static int do_test_code_reading(bool try_kcore)
pr_debug("map__load failed\n");
goto out_err;
}
have_vmlinux = dso__is_vmlinux(map->dso);
have_kcore = dso__is_kcore(map->dso);
dso = map__dso(map);
have_vmlinux = dso__is_vmlinux(dso);
have_kcore = dso__is_kcore(dso);

/* 2nd time through we just try kcore */
if (try_kcore && !have_kcore)
Expand Down
Loading

0 comments on commit 63df0e4

Please sign in to comment.