Currently, libunwind operations are fixed, and they are chosen
according to the host architecture. This will lead a problem that if a
thread is run as x86_32 on x86_64 machine, perf will use libunwind
methods for x86_64 to parse the callchain and get wrong result.

This patch changes the fixed methods of libunwind operations to
thread/map related, and each thread can have indivadual libunwind
operations. Local libunwind methods are registered as default value.

Signed-off-by: He Kuang <heku...@huawei.com>
---
 tools/perf/util/thread.c           |  6 ++++
 tools/perf/util/thread.h           |  4 ++-
 tools/perf/util/unwind-libunwind.c | 58 +++++++++++++++++++++++++++++++++++---
 tools/perf/util/unwind.h           | 19 +++++++++++++
 4 files changed, 82 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 45fcb71..95ff1b8 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -43,6 +43,12 @@ struct thread *thread__new(pid_t pid, pid_t tid)
                thread->cpu = -1;
                INIT_LIST_HEAD(&thread->comm_list);
 
+#ifdef HAVE_LIBUNWIND_SUPPORT
+               unwind__register_ops(thread, local_unwind_libunwind_ops);
+#else
+               unwind__register_ops(thread, NULL);
+#endif
+
                if (unwind__prepare_access(thread) < 0)
                        goto err_thread;
 
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index aa3a8ff..152fb9a 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -11,6 +11,7 @@
 #include <intlist.h>
 
 struct thread_stack;
+struct unwind_libunwind_ops;
 
 struct thread {
        union {
@@ -33,7 +34,8 @@ struct thread {
        void                    *priv;
        struct thread_stack     *ts;
 #ifdef HAVE_LIBUNWIND_SUPPORT
-       void                    *addr_space;
+       void                            *addr_space;
+       struct unwind_libunwind_ops     *unwind_libunwind_ops;
 #endif
 };
 
diff --git a/tools/perf/util/unwind-libunwind.c 
b/tools/perf/util/unwind-libunwind.c
index 63687d3..77b8521 100644
--- a/tools/perf/util/unwind-libunwind.c
+++ b/tools/perf/util/unwind-libunwind.c
@@ -579,7 +579,7 @@ static unw_accessors_t accessors = {
        .get_proc_name          = get_proc_name,
 };
 
-int unwind__prepare_access(struct thread *thread)
+static int _unwind__prepare_access(struct thread *thread)
 {
        if (callchain_param.record_mode != CALLCHAIN_DWARF)
                return 0;
@@ -594,7 +594,7 @@ int unwind__prepare_access(struct thread *thread)
        return 0;
 }
 
-void unwind__flush_access(struct thread *thread)
+static void _unwind__flush_access(struct thread *thread)
 {
        if (callchain_param.record_mode != CALLCHAIN_DWARF)
                return;
@@ -602,7 +602,7 @@ void unwind__flush_access(struct thread *thread)
        unw_flush_cache(thread->addr_space, 0, 0);
 }
 
-void unwind__finish_access(struct thread *thread)
+static void _unwind__finish_access(struct thread *thread)
 {
        if (callchain_param.record_mode != CALLCHAIN_DWARF)
                return;
@@ -662,7 +662,7 @@ static int get_entries(struct unwind_info *ui, 
unwind_entry_cb_t cb,
        return ret;
 }
 
-int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
+static int _unwind__get_entries(unwind_entry_cb_t cb, void *arg,
                        struct thread *thread,
                        struct perf_sample *data, int max_stack)
 {
@@ -680,3 +680,53 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
 
        return get_entries(&ui, cb, arg, max_stack);
 }
+
+static struct unwind_libunwind_ops
+_unwind_libunwind_ops = {
+       .prepare_access = _unwind__prepare_access,
+       .flush_access   = _unwind__flush_access,
+       .finish_access  = _unwind__finish_access,
+       .get_entries    = _unwind__get_entries,
+};
+
+struct unwind_libunwind_ops *
+local_unwind_libunwind_ops = &_unwind_libunwind_ops;
+
+void unwind__register_ops(struct thread *thread,
+                         struct unwind_libunwind_ops *ops)
+{
+       thread->unwind_libunwind_ops = ops;
+}
+
+int unwind__prepare_access(struct thread *thread)
+{
+       if (thread->unwind_libunwind_ops)
+               return thread->unwind_libunwind_ops->prepare_access(thread);
+       else
+               return 0;
+}
+
+void unwind__flush_access(struct thread *thread)
+{
+       if (thread->unwind_libunwind_ops)
+               thread->unwind_libunwind_ops->flush_access(thread);
+}
+
+void unwind__finish_access(struct thread *thread)
+{
+       if (thread->unwind_libunwind_ops)
+               thread->unwind_libunwind_ops->finish_access(thread);
+}
+
+int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
+                        struct thread *thread,
+                        struct perf_sample *data, int max_stack)
+{
+       if (thread->unwind_libunwind_ops)
+               return thread->unwind_libunwind_ops->get_entries(cb, arg,
+                                                                thread,
+                                                                data,
+                                                                max_stack);
+       else
+               return 0;
+}
diff --git a/tools/perf/util/unwind.h b/tools/perf/util/unwind.h
index 12790cf..bd7377b 100644
--- a/tools/perf/util/unwind.h
+++ b/tools/perf/util/unwind.h
@@ -14,6 +14,17 @@ struct unwind_entry {
 
 typedef int (*unwind_entry_cb_t)(struct unwind_entry *entry, void *arg);
 
+struct unwind_libunwind_ops {
+       int (*prepare_access)(struct thread *thread);
+       void (*flush_access)(struct thread *thread);
+       void (*finish_access)(struct thread *thread);
+       int (*get_entries)(unwind_entry_cb_t cb, void *arg,
+                          struct thread *thread,
+                          struct perf_sample *data, int max_stack);
+};
+
+struct unwind_libunwind_ops *local_unwind_libunwind_ops;
+
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
                        struct thread *thread,
@@ -24,6 +35,8 @@ int libunwind__arch_reg_id(int regnum);
 int unwind__prepare_access(struct thread *thread);
 void unwind__flush_access(struct thread *thread);
 void unwind__finish_access(struct thread *thread);
+void unwind__register_ops(struct thread *thread,
+                         struct unwind_libunwind_ops *ops);
 #else
 static inline int unwind__prepare_access(struct thread *thread __maybe_unused)
 {
@@ -32,6 +45,9 @@ static inline int unwind__prepare_access(struct thread 
*thread __maybe_unused)
 
 static inline void unwind__flush_access(struct thread *thread __maybe_unused) 
{}
 static inline void unwind__finish_access(struct thread *thread __maybe_unused) 
{}
+static inline void
+unwind__register_ops(struct thread *thread __maybe_unused,
+                    struct unwind_libunwind_ops *ops __maybe_unused) {}
 #endif
 #else
 static inline int
@@ -51,5 +67,8 @@ static inline int unwind__prepare_access(struct thread 
*thread __maybe_unused)
 
 static inline void unwind__flush_access(struct thread *thread __maybe_unused) 
{}
 static inline void unwind__finish_access(struct thread *thread __maybe_unused) 
{}
+static inline void
+unwind__register_ops(struct thread *thread __maybe_unused,
+                    struct unwind_libunwind_ops *ops __maybe_unused) {}
 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
 #endif /* __UNWIND_H */
-- 
1.8.5.2

Reply via email to