perf callchain: Add order support for libunwind DWARF unwinder
authorJiri Olsa <jolsa@redhat.com>
Wed, 18 Nov 2015 07:52:47 +0000 (08:52 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 23 Nov 2015 21:30:41 +0000 (18:30 -0300)
As reported by Milian, currently for DWARF unwind (both libdw and
libunwind) we display callchain in callee order only.

Adding the support to follow callchain order setup to libunwind DWARF
unwinder, so we could get following output for report:

  $ perf record --call-graph dwarf ls
  ...
  $ perf report --no-children --stdio

    39.26%  ls       libc-2.21.so      [.] __strcoll_l
                 |
                 ---__strcoll_l
                    mpsort_with_tmp
                    mpsort_with_tmp
                    sort_files
                    main
                    __libc_start_main
                    _start
                    0

  $ perf report -g caller --no-children --stdio
    ...
    39.26%  ls       libc-2.21.so      [.] __strcoll_l
                 |
                 ---0
                    _start
                    __libc_start_main
                    main
                    sort_files
                    mpsort_with_tmp
                    mpsort_with_tmp
                    __strcoll_l

Based-on-patch-by: Milian Wolff <milian.wolff@kdab.com>
Reported-and-Tested-by: Milian Wolff <milian.wolff@kdab.com>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Wang Nan <wangnan0@huawei.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/20151118075247.GA5416@krava.brq.redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/unwind-libunwind.c

index 0ae8844fe7a67108df6e36f5323a3bde7a2da93b..3c258a0e4092dbb4d78c6cee0a3225b0f127cc1d 100644 (file)
@@ -615,34 +615,47 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
                       void *arg, int max_stack)
 {
        u64 val;
+       unw_word_t ips[max_stack];
        unw_addr_space_t addr_space;
        unw_cursor_t c;
-       int ret;
+       int ret, i = 0;
 
        ret = perf_reg_value(&val, &ui->sample->user_regs, PERF_REG_IP);
        if (ret)
                return ret;
 
-       ret = entry(val, ui->thread, cb, arg);
-       if (ret)
-               return -ENOMEM;
+       ips[i++] = (unw_word_t) val;
 
-       if (--max_stack == 0)
-               return 0;
-
-       addr_space = thread__priv(ui->thread);
-       if (addr_space == NULL)
-               return -1;
+       /*
+        * If we need more than one entry, do the DWARF
+        * unwind itself.
+        */
+       if (max_stack - 1 > 0) {
+               addr_space = thread__priv(ui->thread);
+               if (addr_space == NULL)
+                       return -1;
+
+               ret = unw_init_remote(&c, addr_space, ui);
+               if (ret)
+                       display_error(ret);
+
+               while (!ret && (unw_step(&c) > 0) && i < max_stack) {
+                       unw_get_reg(&c, UNW_REG_IP, &ips[i]);
+                       ++i;
+               }
 
-       ret = unw_init_remote(&c, addr_space, ui);
-       if (ret)
-               display_error(ret);
+               max_stack = i;
+       }
 
-       while (!ret && (unw_step(&c) > 0) && max_stack--) {
-               unw_word_t ip;
+       /*
+        * Display what we got based on the order setup.
+        */
+       for (i = 0; i < max_stack && !ret; i++) {
+               int j = i;
 
-               unw_get_reg(&c, UNW_REG_IP, &ip);
-               ret = ip ? entry(ip, ui->thread, cb, arg) : 0;
+               if (callchain_param.order == ORDER_CALLER)
+                       j = max_stack - i - 1;
+               ret = ips[j] ? entry(ips[j], ui->thread, cb, arg) : 0;
        }
 
        return ret;