Skip to content

Commit b251e97

Browse files
johnkeepinggregkh
authored andcommitted
perf unwind: Fix libunwind when tid != pid
[ Upstream commit e8ba290 ] Commit e5adfc3 ("perf map: Synthesize maps only for thread group leader") changed the recording side so that we no longer get mmap events for threads other than the thread group leader (when synthesising these events for threads which exist before perf is started). When a file recorded after this change is loaded, the lack of mmap records mean that unwinding is not set up for any other threads. This can be seen in a simple record/report scenario: perf record --call-graph=dwarf -t $TID perf report If $TID is a process ID then the report will show call graphs, but if $TID is a secondary thread the output is as if --call-graph=none was specified. Following the rationale in that commit, move the libunwind fields into struct map_groups and update the libunwind functions to take this instead of the struct thread. This is only required for unwind__finish_access which must now be called from map_groups__delete and the others are changed for symmetry. Note that unwind__get_entries keeps the thread argument since it is required for symbol lookup and the libdw unwind provider uses the thread ID. Signed-off-by: John Keeping <john@metanate.com> Reviewed-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Fixes: e5adfc3 ("perf map: Synthesize maps only for thread group leader") Link: http://lkml.kernel.org/r/20190815100146.28842-2-john@metanate.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2bc1f53 commit b251e97

7 files changed

Lines changed: 48 additions & 47 deletions

File tree

‎tools/perf/util/map.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ struct map_groups *map_groups__new(struct machine *machine)
641641
void map_groups__delete(struct map_groups *mg)
642642
{
643643
map_groups__exit(mg);
644+
unwind__finish_access(mg);
644645
free(mg);
645646
}
646647

@@ -881,7 +882,7 @@ int map_groups__clone(struct thread *thread, struct map_groups *parent)
881882
if (new == NULL)
882883
goto out_unlock;
883884

884-
err = unwind__prepare_access(thread, new, NULL);
885+
err = unwind__prepare_access(mg, new, NULL);
885886
if (err)
886887
goto out_unlock;
887888

‎tools/perf/util/map_groups.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct map_groups {
3131
struct maps maps;
3232
struct machine *machine;
3333
refcount_t refcnt;
34+
#ifdef HAVE_LIBUNWIND_SUPPORT
35+
void *addr_space;
36+
struct unwind_libunwind_ops *unwind_libunwind_ops;
37+
#endif
3438
};
3539

3640
#define KMAP_NAME_LEN 256

‎tools/perf/util/thread.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ void thread__delete(struct thread *thread)
105105
}
106106
up_write(&thread->comm_lock);
107107

108-
unwind__finish_access(thread);
109108
nsinfo__zput(thread->nsinfo);
110109
srccode_state_free(&thread->srccode_state);
111110

@@ -235,7 +234,7 @@ static int ____thread__set_comm(struct thread *thread, const char *str,
235234
list_add(&new->list, &thread->comm_list);
236235

237236
if (exec)
238-
unwind__flush_access(thread);
237+
unwind__flush_access(thread->mg);
239238
}
240239

241240
thread->comm_set = true;
@@ -315,7 +314,7 @@ int thread__insert_map(struct thread *thread, struct map *map)
315314
{
316315
int ret;
317316

318-
ret = unwind__prepare_access(thread, map, NULL);
317+
ret = unwind__prepare_access(thread->mg, map, NULL);
319318
if (ret)
320319
return ret;
321320

@@ -335,7 +334,7 @@ static int __thread__prepare_access(struct thread *thread)
335334
down_read(&maps->lock);
336335

337336
for (map = maps__first(maps); map; map = map__next(map)) {
338-
err = unwind__prepare_access(thread, map, &initialized);
337+
err = unwind__prepare_access(thread->mg, map, &initialized);
339338
if (err || initialized)
340339
break;
341340
}

‎tools/perf/util/thread.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ struct thread {
4444
struct thread_stack *ts;
4545
struct nsinfo *nsinfo;
4646
struct srccode_state srccode_state;
47-
#ifdef HAVE_LIBUNWIND_SUPPORT
48-
void *addr_space;
49-
struct unwind_libunwind_ops *unwind_libunwind_ops;
50-
#endif
5147
bool filter;
5248
int filter_entry_depth;
5349
};

‎tools/perf/util/unwind-libunwind-local.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,26 +615,26 @@ static unw_accessors_t accessors = {
615615
.get_proc_name = get_proc_name,
616616
};
617617

618-
static int _unwind__prepare_access(struct thread *thread)
618+
static int _unwind__prepare_access(struct map_groups *mg)
619619
{
620-
thread->addr_space = unw_create_addr_space(&accessors, 0);
621-
if (!thread->addr_space) {
620+
mg->addr_space = unw_create_addr_space(&accessors, 0);
621+
if (!mg->addr_space) {
622622
pr_err("unwind: Can't create unwind address space.\n");
623623
return -ENOMEM;
624624
}
625625

626-
unw_set_caching_policy(thread->addr_space, UNW_CACHE_GLOBAL);
626+
unw_set_caching_policy(mg->addr_space, UNW_CACHE_GLOBAL);
627627
return 0;
628628
}
629629

630-
static void _unwind__flush_access(struct thread *thread)
630+
static void _unwind__flush_access(struct map_groups *mg)
631631
{
632-
unw_flush_cache(thread->addr_space, 0, 0);
632+
unw_flush_cache(mg->addr_space, 0, 0);
633633
}
634634

635-
static void _unwind__finish_access(struct thread *thread)
635+
static void _unwind__finish_access(struct map_groups *mg)
636636
{
637-
unw_destroy_addr_space(thread->addr_space);
637+
unw_destroy_addr_space(mg->addr_space);
638638
}
639639

640640
static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
@@ -659,7 +659,7 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
659659
*/
660660
if (max_stack - 1 > 0) {
661661
WARN_ONCE(!ui->thread, "WARNING: ui->thread is NULL");
662-
addr_space = ui->thread->addr_space;
662+
addr_space = ui->thread->mg->addr_space;
663663

664664
if (addr_space == NULL)
665665
return -1;

‎tools/perf/util/unwind-libunwind.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops;
1111
struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops;
1212
struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops;
1313

14-
static void unwind__register_ops(struct thread *thread,
14+
static void unwind__register_ops(struct map_groups *mg,
1515
struct unwind_libunwind_ops *ops)
1616
{
17-
thread->unwind_libunwind_ops = ops;
17+
mg->unwind_libunwind_ops = ops;
1818
}
1919

20-
int unwind__prepare_access(struct thread *thread, struct map *map,
20+
int unwind__prepare_access(struct map_groups *mg, struct map *map,
2121
bool *initialized)
2222
{
2323
const char *arch;
@@ -28,7 +28,7 @@ int unwind__prepare_access(struct thread *thread, struct map *map,
2828
if (!dwarf_callchain_users)
2929
return 0;
3030

31-
if (thread->addr_space) {
31+
if (mg->addr_space) {
3232
pr_debug("unwind: thread map already set, dso=%s\n",
3333
map->dso->name);
3434
if (initialized)
@@ -37,14 +37,14 @@ int unwind__prepare_access(struct thread *thread, struct map *map,
3737
}
3838

3939
/* env->arch is NULL for live-mode (i.e. perf top) */
40-
if (!thread->mg->machine->env || !thread->mg->machine->env->arch)
40+
if (!mg->machine->env || !mg->machine->env->arch)
4141
goto out_register;
4242

43-
dso_type = dso__type(map->dso, thread->mg->machine);
43+
dso_type = dso__type(map->dso, mg->machine);
4444
if (dso_type == DSO__TYPE_UNKNOWN)
4545
return 0;
4646

47-
arch = perf_env__arch(thread->mg->machine->env);
47+
arch = perf_env__arch(mg->machine->env);
4848

4949
if (!strcmp(arch, "x86")) {
5050
if (dso_type != DSO__TYPE_64BIT)
@@ -59,37 +59,37 @@ int unwind__prepare_access(struct thread *thread, struct map *map,
5959
return 0;
6060
}
6161
out_register:
62-
unwind__register_ops(thread, ops);
62+
unwind__register_ops(mg, ops);
6363

64-
err = thread->unwind_libunwind_ops->prepare_access(thread);
64+
err = mg->unwind_libunwind_ops->prepare_access(mg);
6565
if (initialized)
6666
*initialized = err ? false : true;
6767
return err;
6868
}
6969

70-
void unwind__flush_access(struct thread *thread)
70+
void unwind__flush_access(struct map_groups *mg)
7171
{
7272
if (!dwarf_callchain_users)
7373
return;
7474

75-
if (thread->unwind_libunwind_ops)
76-
thread->unwind_libunwind_ops->flush_access(thread);
75+
if (mg->unwind_libunwind_ops)
76+
mg->unwind_libunwind_ops->flush_access(mg);
7777
}
7878

79-
void unwind__finish_access(struct thread *thread)
79+
void unwind__finish_access(struct map_groups *mg)
8080
{
8181
if (!dwarf_callchain_users)
8282
return;
8383

84-
if (thread->unwind_libunwind_ops)
85-
thread->unwind_libunwind_ops->finish_access(thread);
84+
if (mg->unwind_libunwind_ops)
85+
mg->unwind_libunwind_ops->finish_access(mg);
8686
}
8787

8888
int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
8989
struct thread *thread,
9090
struct perf_sample *data, int max_stack)
9191
{
92-
if (thread->unwind_libunwind_ops)
93-
return thread->unwind_libunwind_ops->get_entries(cb, arg, thread, data, max_stack);
92+
if (thread->mg->unwind_libunwind_ops)
93+
return thread->mg->unwind_libunwind_ops->get_entries(cb, arg, thread, data, max_stack);
9494
return 0;
9595
}

‎tools/perf/util/unwind.h‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/types.h>
77

88
struct map;
9+
struct map_groups;
910
struct perf_sample;
1011
struct symbol;
1112
struct thread;
@@ -19,9 +20,9 @@ struct unwind_entry {
1920
typedef int (*unwind_entry_cb_t)(struct unwind_entry *entry, void *arg);
2021

2122
struct unwind_libunwind_ops {
22-
int (*prepare_access)(struct thread *thread);
23-
void (*flush_access)(struct thread *thread);
24-
void (*finish_access)(struct thread *thread);
23+
int (*prepare_access)(struct map_groups *mg);
24+
void (*flush_access)(struct map_groups *mg);
25+
void (*finish_access)(struct map_groups *mg);
2526
int (*get_entries)(unwind_entry_cb_t cb, void *arg,
2627
struct thread *thread,
2728
struct perf_sample *data, int max_stack);
@@ -46,20 +47,20 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
4647
#endif
4748

4849
int LIBUNWIND__ARCH_REG_ID(int regnum);
49-
int unwind__prepare_access(struct thread *thread, struct map *map,
50+
int unwind__prepare_access(struct map_groups *mg, struct map *map,
5051
bool *initialized);
51-
void unwind__flush_access(struct thread *thread);
52-
void unwind__finish_access(struct thread *thread);
52+
void unwind__flush_access(struct map_groups *mg);
53+
void unwind__finish_access(struct map_groups *mg);
5354
#else
54-
static inline int unwind__prepare_access(struct thread *thread __maybe_unused,
55+
static inline int unwind__prepare_access(struct map_groups *mg __maybe_unused,
5556
struct map *map __maybe_unused,
5657
bool *initialized __maybe_unused)
5758
{
5859
return 0;
5960
}
6061

61-
static inline void unwind__flush_access(struct thread *thread __maybe_unused) {}
62-
static inline void unwind__finish_access(struct thread *thread __maybe_unused) {}
62+
static inline void unwind__flush_access(struct map_groups *mg __maybe_unused) {}
63+
static inline void unwind__finish_access(struct map_groups *mg __maybe_unused) {}
6364
#endif
6465
#else
6566
static inline int
@@ -72,14 +73,14 @@ unwind__get_entries(unwind_entry_cb_t cb __maybe_unused,
7273
return 0;
7374
}
7475

75-
static inline int unwind__prepare_access(struct thread *thread __maybe_unused,
76+
static inline int unwind__prepare_access(struct map_groups *mg __maybe_unused,
7677
struct map *map __maybe_unused,
7778
bool *initialized __maybe_unused)
7879
{
7980
return 0;
8081
}
8182

82-
static inline void unwind__flush_access(struct thread *thread __maybe_unused) {}
83-
static inline void unwind__finish_access(struct thread *thread __maybe_unused) {}
83+
static inline void unwind__flush_access(struct map_groups *mg __maybe_unused) {}
84+
static inline void unwind__finish_access(struct map_groups *mg __maybe_unused) {}
8485
#endif /* HAVE_DWARF_UNWIND_SUPPORT */
8586
#endif /* __UNWIND_H */

0 commit comments

Comments
 (0)