Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp3448040imd; Mon, 29 Oct 2018 07:18:21 -0700 (PDT) X-Google-Smtp-Source: AJdET5fvELiVg862SYjm1oKN3NoF5IET/awe4cSuN7vDGI+QRlWVVtJzKBN3/NAnFtPrfpHwWtX5 X-Received: by 2002:a63:3dcb:: with SMTP id k194-v6mr13951166pga.191.1540822701489; Mon, 29 Oct 2018 07:18:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540822701; cv=none; d=google.com; s=arc-20160816; b=Iyo0WR9PN5E1zm0YTxiv9Eq5DBiKbOBBH+XI9E/pfCm9PfglNwS/V21TJ1i/E2sE4Z ds3CNxo3T3SuOwuaZYERXbDNUgH9+ObNy2w6AxxldigL/6+JmSQZl68hF6k/fQsHdohn nHEYuvZFyMBhnZZMvX7P1+Aj+NwqWdiuGRpySE41QPP3duQRW37kjupZLSl+FJyjCJYA y53qOk4Hvn1XHJ0Kyr0bEUC4E1cBCsZMa+7f+PBW6tJ0KSCCa0kXwIQggnmZciKDzbOq yRVDw5D6rNnDmddsGdlQvV6Zif5HCOjWVDDbyUXwWfTyRPdT3olqgScG5qn9VMUQSBBy nz3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=dXtYbeCJac6p1kSNnQVYPPJ7E6fBosnUyKfJD2tGn7Q=; b=0Jp+j+uFZGf2MDuqwF0K++LWT5kHxNFLo1bznKcWxJ7NLPD2HZpvf1j542JP3Fk6px UE9BKhFZQMDxyQS3z3GL83TxQ1DPRQUDXeJZ7YeTLlWxbFkenNd5tv2oVSShgNRHhrHA FABVKkostu/dRZLHKxAAIAmRJq8QSb2/8/tT1+sX4MU63NUsKlAK/+cym2P6CjlQx2AF qmFh2ImeSWysNMnqrTpttQQEqmEopzc1u5+0ODw5VGRtzEW+Nr3RGODaZ6k6IhPCiS9j /Zff/9qojKPFBPzQLs3h9kbRfg9yNarULiy/Sf7AIGseBcHh4TP2qKLJ9UWTVcMwxFqj XFEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kdab.com header.s=dkim header.b=bDNb5ZWB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=kdab.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f88-v6si21338191pfe.243.2018.10.29.07.18.05; Mon, 29 Oct 2018 07:18:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kdab.com header.s=dkim header.b=bDNb5ZWB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=kdab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726493AbeJ2XFm (ORCPT + 99 others); Mon, 29 Oct 2018 19:05:42 -0400 Received: from mail.kdab.com ([176.9.126.58]:27684 "EHLO mail.kdab.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725920AbeJ2XFl (ORCPT ); Mon, 29 Oct 2018 19:05:41 -0400 Authentication-Results: mail.kdab.com (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=kdab.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kdab.com; h= content-transfer-encoding:mime-version:message-id:date:date :subject:subject:from:from; s=dkim; t=1540822606; x=1541686607; bh=dXtYbeCJac6p1kSNnQVYPPJ7E6fBosnUyKfJD2tGn7Q=; b=bDNb5ZWBZFOS 1reDDTiUN/qSYHyfZQ63UaybJbjxE4W/gYsooLVH3dTp4E4utUgqUJ58Yc3rcM0J 0KrRAlFXGOjjqPEy4JUqLC4ZfDpkmKOJGeBRvKsKY8kKKbiIhDzoMdYG1BGKTs+F /m14oZCcvqHlv7lU3YHJMd+F9Xp1dxk= X-Virus-Scanned: amavisd-new at kdab.com From: Milian Wolff To: acme@kernel.org, jolsa@kernel.org Cc: Linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Milian Wolff Subject: [PATCH] perf util: take pgoff into account when reporting elf to libdwfl Date: Mon, 29 Oct 2018 15:16:44 +0100 Message-Id: <20181029141644.3907-1-milian.wolff@kdab.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Libdwfl parses an ELF file itself and creates mappings for the individual sections. Perf on the other hand sees raw mmap events which represent individual sections. When we encounter an address pointing into a mapping with pgoff !=3D 0, we must take that into account and report the file at the non-offset base address. This fixes unwinding with libdwfl in some cases. E.g. for a file like: ``` #include #include #include #include #include #include #include #include using namespace std; mutex g_mutex; double worker() { lock_guard guard(g_mutex); uniform_real_distribution uniform(-1E5, 1E5); default_random_engine engine; double s =3D 0; for (int i =3D 0; i < 1000; ++i) { s +=3D norm(complex(uniform(engine), uniform(engine))); } cout << s << endl; return s; } int main() { vector> results; for (int i =3D 0; i < 10000; ++i) { results.push_back(async(launch::async, worker)); } return 0; } ``` Compile it with `g++ -g -O2`, then record it with `perf record --call-graph dwarf -e sched:sched_switch`. When you analyze it with `perf script` and libunwind, you should see: ``` cpp-locking 20038 [005] 54830.236589: sched:sched_switch: prev_comm=3Dcpp= -locking prev_pid=3D20038 prev_prio=3D120 prev_state=3DT =3D=3D> next_com= m=3Dswapper/5 next_pid=3D0 next_prio=3D120 ffffffffb166fec5 __sched_text_start+0x545 (/lib/modules/4.14.78-1= -lts/build/vmlinux) ffffffffb166fec5 __sched_text_start+0x545 (/lib/modules/4.14.78-1= -lts/build/vmlinux) ffffffffb1670208 schedule+0x28 (/lib/modules/4.14.78-1-lts/build/= vmlinux) ffffffffb16737cc rwsem_down_read_failed+0xec (/lib/modules/4.14.7= 8-1-lts/build/vmlinux) ffffffffb1665e04 call_rwsem_down_read_failed+0x14 (/lib/modules/4= .14.78-1-lts/build/vmlinux) ffffffffb1672a03 down_read+0x13 (/lib/modules/4.14.78-1-lts/build= /vmlinux) ffffffffb106bd85 __do_page_fault+0x445 (/lib/modules/4.14.78-1-lt= s/build/vmlinux) ffffffffb18015f5 page_fault+0x45 (/lib/modules/4.14.78-1-lts/buil= d/vmlinux) 7f38e4252591 new_heap+0x101 (/usr/lib/libc-2.28.so) 7f38e4252d0b arena_get2.part.4+0x2fb (/usr/lib/libc-2.28.so) 7f38e4255b1c tcache_init.part.6+0xec (/usr/lib/libc-2.28.so) 7f38e42569e5 __GI___libc_malloc+0x115 (inlined) 7f38e4241790 __GI__IO_file_doallocate+0x90 (inlined) 7f38e424fbbf __GI__IO_doallocbuf+0x4f (inlined) 7f38e424ee47 __GI__IO_file_overflow+0x197 (inlined) 7f38e424df36 _IO_new_file_xsputn+0x116 (inlined) 7f38e4242bfb __GI__IO_fwrite+0xdb (inlined) 7f38e463fa6d std::basic_streambuf >::sputn(char const*, long)+0x1cd (inlined) 7f38e463fa6d std::ostreambuf_iterator >::_M_put(char const*, long)+0x1cd (inlined) 7f38e463fa6d std::ostreambuf_iterator > std::__write(std::ostreambuf_iterator >, char const*, int)+0x1cd (inlined) 7f38e463fa6d std::ostreambuf_iterator > std::num_put > >::_M_insert_float(std::ostreambuf_iterator 7f38e464bd70 std::num_put > >::put(std::ostreambuf_iterator >, std::ios_base&, char, double) const+0x90 (inl> 7f38e464bd70 std::ostream& std::ostream::_M_insert(do= uble)+0x90 (/usr/lib/libstdc++.so.6.0.25) 563b9cb502f7 std::ostream::operator<<(double)+0xb7 (inlined) 563b9cb502f7 worker()+0xb7 (/ssd/milian/projects/kdab/rnd/hot= spot/build/tests/test-clients/cpp-locking/cpp-locking) 563b9cb506fb double std::__invoke_impl(= std::__invoke_other, double (*&&)())+0x2b (inlined) 563b9cb506fb std::__invoke_result::type std::__= invoke(double (*&&)())+0x2b (inlined) 563b9cb506fb decltype (__invoke((_S_declval<0ul>)())) std::th= read::_Invoker >::_M_invoke<0ul>(std::_Index_tup= le<0ul>)+0x2b (inlined) 563b9cb506fb std::thread::_Invoker >= ::operator()()+0x2b (inlined) 563b9cb506fb std::__future_base::_Task_setter, std::__future_base::_Result_base::_= Deleter>, std::thread::_Invoker >, dou> 563b9cb506fb std::_Function_handler (), = std::__future_base::_Task_setter 563b9cb507e8 std::function ()>::operator= ()() const+0x28 (inlined) 563b9cb507e8 std::__future_base::_State_baseV2::_M_do_set(std= ::function ()>*, bool*)+0x28 (/ssd/milian/> 7f38e46d24fe __pthread_once_slow+0xbe (/usr/lib/libpthread-2.= 28.so) 563b9cb51149 __gthread_once+0xe9 (inlined) 563b9cb51149 void std::call_once ()>*, bool*)> 563b9cb51149 std::__future_base::_State_baseV2::_M_set_result= (std::function ()>, bool)+0xe9 (inlined) 563b9cb51149 std::__future_base::_Async_state_impl >, double>::_Async_state_impl(std::t= hread::_Invoker >&&)::{lambda()#1}::op> 563b9cb51149 void std::__invoke_impl >, dou= ble>::_Async_state_impl(std::thread::_Invoker 563b9cb51149 std::__invoke_result >, double>::_As= ync_state_impl(std::thread::_Invoker >> 563b9cb51149 decltype (__invoke((_S_declval<0ul>)())) std::th= read::_Invoker >, double>::_Async_state_> 563b9cb51149 std::thread::_Invoker >, = double>::_Async_state_impl(std::thread::_Invoker 563b9cb51149 std::thread::_State_impl >, double>::_Async_state_impl(std::thread> 7f38e45f0062 execute_native_thread_routine+0x12 (/usr/lib/lib= stdc++.so.6.0.25) 7f38e46caa9c start_thread+0xfc (/usr/lib/libpthread-2.28.so) 7f38e42ccb22 __GI___clone+0x42 (inlined) ``` Before this patch, using libdwfl, you would see: ``` cpp-locking 20038 [005] 54830.236589: sched:sched_switch: prev_comm=3Dcpp= -locking prev_pid=3D20038 prev_prio=3D120 prev_state=3DT =3D=3D> next_com= m=3Dswapper/5 next_pid=3D0 next_prio=3D120 ffffffffb166fec5 __sched_text_start+0x545 (/lib/modules/4.14.78-1= -lts/build/vmlinux) ffffffffb166fec5 __sched_text_start+0x545 (/lib/modules/4.14.78-1= -lts/build/vmlinux) ffffffffb1670208 schedule+0x28 (/lib/modules/4.14.78-1-lts/build/= vmlinux) ffffffffb16737cc rwsem_down_read_failed+0xec (/lib/modules/4.14.7= 8-1-lts/build/vmlinux) ffffffffb1665e04 call_rwsem_down_read_failed+0x14 (/lib/modules/4= .14.78-1-lts/build/vmlinux) ffffffffb1672a03 down_read+0x13 (/lib/modules/4.14.78-1-lts/build= /vmlinux) ffffffffb106bd85 __do_page_fault+0x445 (/lib/modules/4.14.78-1-lt= s/build/vmlinux) ffffffffb18015f5 page_fault+0x45 (/lib/modules/4.14.78-1-lts/buil= d/vmlinux) 7f38e4252591 new_heap+0x101 (/usr/lib/libc-2.28.so) a041161e77950c5c [unknown] ([unknown]) ``` With this patch applied, we get a bit further in unwinding: ``` cpp-locking 20038 [005] 54830.236589: sched:sched_switch: prev_comm=3Dcpp= -locking prev_pid=3D20038 prev_prio=3D120 prev_state=3DT =3D=3D> next_com= m=3Dswapper/5 next_pid=3D0 next_prio=3D120 ffffffffb166fec5 __sched_text_start+0x545 (/lib/modules/4.14.78-1= -lts/build/vmlinux) ffffffffb166fec5 __sched_text_start+0x545 (/lib/modules/4.14.78-1= -lts/build/vmlinux) ffffffffb1670208 schedule+0x28 (/lib/modules/4.14.78-1-lts/build/= vmlinux) ffffffffb16737cc rwsem_down_read_failed+0xec (/lib/modules/4.14.7= 8-1-lts/build/vmlinux) ffffffffb1665e04 call_rwsem_down_read_failed+0x14 (/lib/modules/4= .14.78-1-lts/build/vmlinux) ffffffffb1672a03 down_read+0x13 (/lib/modules/4.14.78-1-lts/build= /vmlinux) ffffffffb106bd85 __do_page_fault+0x445 (/lib/modules/4.14.78-1-lt= s/build/vmlinux) ffffffffb18015f5 page_fault+0x45 (/lib/modules/4.14.78-1-lts/buil= d/vmlinux) 7f38e4252591 new_heap+0x101 (/usr/lib/libc-2.28.so) 7f38e4252d0b arena_get2.part.4+0x2fb (/usr/lib/libc-2.28.so) 7f38e4255b1c tcache_init.part.6+0xec (/usr/lib/libc-2.28.so) 7f38e42569e5 __GI___libc_malloc+0x115 (inlined) 7f38e4241790 __GI__IO_file_doallocate+0x90 (inlined) 7f38e424fbbf __GI__IO_doallocbuf+0x4f (inlined) 7f38e424ee47 __GI__IO_file_overflow+0x197 (inlined) 7f38e424df36 _IO_new_file_xsputn+0x116 (inlined) 7f38e4242bfb __GI__IO_fwrite+0xdb (inlined) 7f38e463fa6d std::basic_streambuf >::sputn(char const*, long)+0x1cd (inlined) 7f38e463fa6d std::ostreambuf_iterator >::_M_put(char const*, long)+0x1cd (inlined) 7f38e463fa6d std::ostreambuf_iterator > std::__write(std::ostreambuf_iterator >, char const*, int)+0x1cd (inlined) 7f38e463fa6d std::ostreambuf_iterator > std::num_put > >::_M_insert_float(std::ostreambuf_iterator 7f38e464bd70 std::num_put > >::put(std::ostreambuf_iterator >, std::ios_base&, char, double) const+0x90 (inl> 7f38e464bd70 std::ostream& std::ostream::_M_insert(do= uble)+0x90 (/usr/lib/libstdc++.so.6.0.25) 563b9cb502f7 std::ostream::operator<<(double)+0xb7 (inlined) 563b9cb502f7 worker()+0xb7 (/ssd/milian/projects/kdab/rnd/hot= spot/build/tests/test-clients/cpp-locking/cpp-locking) 6eab825c1ee3e4ff [unknown] ([unknown]) ``` Note that the backtrace is still stopping too early, when compared to the nice results obtained via libunwind. It's unclear so far what the reason for that is. Signed-off-by: Milian Wolff Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa --- tools/perf/util/unwind-libdw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libd= w.c index 6f318b15950e..5eff9bfc5758 100644 --- a/tools/perf/util/unwind-libdw.c +++ b/tools/perf/util/unwind-libdw.c @@ -45,13 +45,13 @@ static int __report_module(struct addr_location *al, = u64 ip, Dwarf_Addr s; =20 dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL); - if (s !=3D al->map->start) + if (s !=3D al->map->start - al->map->pgoff) mod =3D 0; } =20 if (!mod) mod =3D dwfl_report_elf(ui->dwfl, dso->short_name, - (dso->symsrc_filename ? dso->symsrc_filename : dso->long_name)= , -1, al->map->start, + (dso->symsrc_filename ? dso->symsrc_filename : dso->long_name)= , -1, al->map->start - al->map->pgoff, false); =20 return mod && dwfl_addrmodule(ui->dwfl, ip) =3D=3D mod ? 0 : -1; --=20 2.19.1