From patchwork Tue Nov 1 13:48:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13675 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2978271wru; Tue, 1 Nov 2022 06:52:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Z4NytuqYq6WpCfkU4pn025o1UAXAJW2PXVJt3N90hxB4iYYiGM7B4Ta/ijn7QJ7tIede7 X-Received: by 2002:aa7:cf83:0:b0:463:1b7d:e318 with SMTP id z3-20020aa7cf83000000b004631b7de318mr14887513edx.4.1667310747935; Tue, 01 Nov 2022 06:52:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667310747; cv=none; d=google.com; s=arc-20160816; b=W7W9lExZ+DWFcHnt61UrE19u2o6a6Tp+s49X7N/XzSTtGjg60suhrj3ki+AVJwl1Pj ocPalUAHjiM++o3Arg/7uQ6Qr7eUaR/Rrp5iiJoi0+MmdAU1vhs5i7l6zko1VYAO3auo MT6YPZ0ngIT4bwaSbT0a+EVvtes4PP4qFS1d9wyyifuc68E+5n2n5jW2bMCkrrcuIYrz i3Abou+s5f3OGQJF/o/gA+AALP51hFIfmOpOkYXVloM1zCuIulzEAYXvcDjvTZIvvM8j SDtyG0WT7C8a7EQqvyRtfTVvyEbfLSmEYMW/aXuXMaQoKQ3hcScRdwLB9KQDyMdMuWFu sHDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=OykKU95q1fW+f+5QcJ+FEAsEwyRORGLiGVLzPrN/6Fc=; b=CjdOQN48DOrvmrzBg0f8PnIvK4I80V8riMTYg80j3m3rT/PQUbV17NQ1QKQZ99DNQA FXtpPH6JmOuFnzp1DVKXQth7P9aQZCTKSb0oFkUhSUKRldZKby7g0LVv+E+SEo/rPTlY Dj+nlMOoQevLLJSyoT0dcIMq5VABicZz8LVuQqG7A7aBzD0JlLmxCBmOvns+daGHxoZ4 TWlYWi6VNY35vezsnuSK+N/OfUHsV2bUckrG/FflFiwjU9bq+ETg8yFvRjQc2WScvHJb XakOVG5CIIL5DTf+ZPe3DtG+zQFwnWSjK+xGW2tPE60WSLhDkdHvVUEKUxhenKR1UbUV 7y1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=huayVJFL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k14-20020a170906a38e00b007ad9adaf33dsi9107549ejz.372.2022.11.01.06.52.04; Tue, 01 Nov 2022 06:52:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=huayVJFL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229742AbiKANso (ORCPT + 99 others); Tue, 1 Nov 2022 09:48:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230039AbiKANsk (ORCPT ); Tue, 1 Nov 2022 09:48:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CA226144; Tue, 1 Nov 2022 06:48:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2C03461225; Tue, 1 Nov 2022 13:48:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CAC07C433C1; Tue, 1 Nov 2022 13:48:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667310514; bh=4zGhVDzrhZqHoaFUVOpPlmh8pfQtAUkt30QT/bMRjM0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=huayVJFL+SLzOacxRHKmYJOkjnnZLs1ERqTtrW6FrTcsufVJi+zQhqqhNAh9tZgJ3 i9qwnqHI7+kiIFoAiUggjbyi8r+HKg83Pr2uyFOSXUn8nLhsgLhHP7C/Z2VCxjE9IA Z/ww7dPeWMjrdWWcFRwDjBEoN8ghrjEVaG0EL/6XqM6OSuzELbf5uscVDR6Nmmvn8C QCy1KuNK6FKIMFTqJZilCA3Id9gNEHpz8/CRVP0Xkknd2pehbh9bqxUFqLpUhrPb8w wqyQva+prqLcI0FXNmmDTufLfEZO4DEVhHOwXfwQw8TC4WmIvnlKikofbctM+1RE8L AN5+1qpUHRbuA== From: "Masami Hiramatsu (Google)" To: Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Masami Hiramatsu , Steven Rostedt Subject: [PATCH v2 1/3] tools/perf: Fix to avoid crashing if DW_AT_decl_file is NULL Date: Tue, 1 Nov 2022 22:48:30 +0900 Message-Id: <166731051077.2100653.15626653369345128302.stgit@devnote3> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog In-Reply-To: <166731050151.2100653.8202870942871353491.stgit@devnote3> References: <166731050151.2100653.8202870942871353491.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748302034700233983?= X-GMAIL-MSGID: =?utf-8?q?1748302034700233983?= From: Masami Hiramatsu (Google) Since Clang generates the DWARF5 which will set DW_AT_decl_file as 0, dwarf_decl_file() thinks that is invalid and returns NULL. In that case the perf probe will crash by SIGSEGV because it doesn't expect the NULL decl_file. This adds checks of the return value of dwarf_decl_file() to avoid such SEGV with clang DWARF5 file. Without this, perf probe crashes like below; $ ./perf probe -k $BIN_PATH/vmlinux -s $SRC_PATH -L vfs_read:10 Segmentation fault With this, perf probe just warns it; $ ./perf probe -k $BIN_PATH/vmlinux -s $SRC_PATH -L vfs_read:10 Debuginfo analysis failed. Error: Failed to show lines. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Update patch description. --- tools/perf/util/dwarf-aux.c | 7 ++++++- tools/perf/util/probe-finder.c | 29 +++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 609ca1671501..406b7bdc851a 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -137,7 +137,7 @@ int cu_find_lineinfo(Dwarf_Die *cu_die, Dwarf_Addr addr, } out: - return *lineno ?: -ENOENT; + return (*lineno && *fname) ? *lineno : -ENOENT; } static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data); @@ -874,6 +874,11 @@ int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data) cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL); dwarf_decl_line(rt_die, &decl); decf = dwarf_decl_file(rt_die); + if (!decf) { + pr_debug2("Failed to get the declared file name of %s\n", + dwarf_diename(rt_die)); + return -EINVAL; + } } else cu_die = rt_die; if (!cu_die) { diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c index 50d861a80f57..1aa8fcc41c76 100644 --- a/tools/perf/util/probe-finder.c +++ b/tools/perf/util/probe-finder.c @@ -1063,6 +1063,7 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) struct dwarf_callback_param *param = data; struct probe_finder *pf = param->data; struct perf_probe_point *pp = &pf->pev->point; + const char *fname; /* Check tag and diename */ if (!die_is_func_def(sp_die) || @@ -1070,12 +1071,17 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) return DWARF_CB_OK; /* Check declared file */ - if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die))) + fname = dwarf_decl_file(sp_die); + if (!fname) { + pr_warning("A function DIE doesn't have decl_line. Maybe broken DWARF?\n"); + return DWARF_CB_OK; + } + if (pp->file && fname && strtailcmp(pp->file, fname)) return DWARF_CB_OK; pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die), (unsigned long)dwarf_dieoffset(sp_die)); - pf->fname = dwarf_decl_file(sp_die); + pf->fname = fname; if (pp->line) { /* Function relative line */ dwarf_decl_line(sp_die, &pf->lno); pf->lno += pp->line; @@ -1134,6 +1140,7 @@ struct pubname_callback_param { static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) { struct pubname_callback_param *param = data; + const char *fname; if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) { if (dwarf_tag(param->sp_die) != DW_TAG_subprogram) @@ -1143,9 +1150,11 @@ static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die)) return DWARF_CB_OK; - if (param->file && - strtailcmp(param->file, dwarf_decl_file(param->sp_die))) - return DWARF_CB_OK; + if (param->file) { + fname = dwarf_decl_file(param->sp_die); + if (!fname || strtailcmp(param->file, fname)) + return DWARF_CB_OK; + } param->found = 1; return DWARF_CB_ABORT; @@ -1779,7 +1788,7 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, u64 addr, } /* Verify the lineno and baseline are in a same file */ tmp = dwarf_decl_file(&spdie); - if (!tmp || strcmp(tmp, fname) != 0) + if (!tmp || (fname && strcmp(tmp, fname) != 0)) lineno = 0; } @@ -1889,10 +1898,14 @@ static int line_range_search_cb(Dwarf_Die *sp_die, void *data) struct dwarf_callback_param *param = data; struct line_finder *lf = param->data; struct line_range *lr = lf->lr; + const char *fname; /* Check declared file */ - if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die))) - return DWARF_CB_OK; + if (lr->file) { + fname = dwarf_decl_file(sp_die); + if (!fname || strtailcmp(lr->file, fname)) + return DWARF_CB_OK; + } if (die_match_name(sp_die, lr->function) && die_is_func_def(sp_die)) { lf->fname = dwarf_decl_file(sp_die); From patchwork Tue Nov 1 13:48:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13671 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2977487wru; Tue, 1 Nov 2022 06:50:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6bhX2KkxtQ+mx/BsSlN8ytVoK0bFDJof3ykEHvd5UZ2OVm9DEGOAu5BFnplGavMVNgA4q3 X-Received: by 2002:aa7:de0a:0:b0:462:d2a0:93a with SMTP id h10-20020aa7de0a000000b00462d2a0093amr18492436edv.275.1667310659690; Tue, 01 Nov 2022 06:50:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667310659; cv=none; d=google.com; s=arc-20160816; b=E4LA8dtPc7iXhlbQxGyRhHcQKeja18Frzo71wI6ZMJl83vytTiqdJzSoyGYRCFTnDn KlpscbEzAVP0K2f3AtHN/ULSLAcWxfCfUeJNVh5vxMLqh7uatf1OrZv8fS9Rm6KxlTf6 WYqojNJmVlMHZR9Y9xuIgzwGbnC1jl4KuCpy9nPOmGdM1oJjXvzXGQnlgDgKdzm8QlIX ToBwT6faY6Ps8pqpcPXwqQAC1cyYdVXfcsianktZWcql6EdRZP01/ULjwCidevZqo/R/ f5sPt/MAqT9NX96rMEnaQJG7z/vKMbxwCmYUfcoikars+qH03oGsVSYEPWVEPs/pNJqw iXtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wWcmbp/YCO+j4LyJ9CnX/DhA6yih5L+CytKhaSWmHF8=; b=oBt9x+h2r2dDIhLhaataodKrDbziel1WneSrLwG1VWnQIHb+HOGqdQufDdhC8oSurC zA7uPdHqAUi7Aaq013SpcRrVMRlya19nKE9HuDTILX+0/YIRc5r4lkqN3Ky9o/Dwtwvh 5o/Am9JrJJlkc31E0/JkRpVEn1C1SAHlZKOBM80uRaZiqyc+HdcF2FjU8kgepVTFUMwJ zX97ZpGGcIgrTZa7unKh0PfTBrTQzguLT7b/bL+f6vgYiZfm/pj33uwc2RI4iu2yxxmo J8qH1mxVAcp1X0MScc24d5U/Lhfl60qlnahXa3vFlva0SvoDj2yKQQs/4AI1lQ92QzfC maaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qITFJWIw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d38-20020a056402402600b00461ac01753fsi11930163eda.369.2022.11.01.06.50.35; Tue, 01 Nov 2022 06:50:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qITFJWIw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230030AbiKANtH (ORCPT + 99 others); Tue, 1 Nov 2022 09:49:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230018AbiKANsu (ORCPT ); Tue, 1 Nov 2022 09:48:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00AAA6140; Tue, 1 Nov 2022 06:48:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A040061455; Tue, 1 Nov 2022 13:48:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3CE4BC433D6; Tue, 1 Nov 2022 13:48:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667310524; bh=nd0dM0YiGevMBmIw3JCc/91WirhrGmiigVH7OCr9EG8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qITFJWIw5nE67e/MvJ6BMM3CzhJhuhNuGzXbImQKrVLTjW1DVo16XYtnVhwFIWLv2 QkOoTitJELopSzp9tpv3SyZVO4dskXfjpxZFl3U1tBJCc6rRZsWCLkzSayEbyci3Nv 3o57qDMmUFlY29neDnE0gVwAlSp6QXycCuL8FVv8HuVHSdAVyMz+PzDGw4iY4Qfx4f NYxBh4wBKyWP9IJmE13zawMX+R3vBR83g+ROMugKdnBeHY0gyf4n13Z7kobJUR7F3N N1LMyweNgU4mD9CYrhGfDzrRnMLsqTjo/m80vWtvj6X5dihlsU8I/gRgbL9xtg4J/9 bOEIVqbpjk9kA== From: "Masami Hiramatsu (Google)" To: Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Masami Hiramatsu , Steven Rostedt Subject: [PATCH v2 2/3] tools/perf: Fix to use dwarf_attr_integrate for generic attr accessor Date: Tue, 1 Nov 2022 22:48:39 +0900 Message-Id: <166731051988.2100653.13595339994343449770.stgit@devnote3> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog In-Reply-To: <166731050151.2100653.8202870942871353491.stgit@devnote3> References: <166731050151.2100653.8202870942871353491.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748301942285039219?= X-GMAIL-MSGID: =?utf-8?q?1748301942285039219?= From: Masami Hiramatsu (Google) Use dwarf_attr_integrate() instead of dwarf_attr() for generic attribute acccessor functions, so that it can find the specified attribute from abstact origin DIE etc. Signed-off-by: Masami Hiramatsu (Google) --- tools/perf/util/dwarf-aux.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 406b7bdc851a..216fc3d959e8 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -308,7 +308,7 @@ static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name, { Dwarf_Attribute attr; - if (dwarf_attr(tp_die, attr_name, &attr) == NULL || + if (dwarf_attr_integrate(tp_die, attr_name, &attr) == NULL || dwarf_formudata(&attr, result) != 0) return -ENOENT; @@ -321,7 +321,7 @@ static int die_get_attr_sdata(Dwarf_Die *tp_die, unsigned int attr_name, { Dwarf_Attribute attr; - if (dwarf_attr(tp_die, attr_name, &attr) == NULL || + if (dwarf_attr_integrate(tp_die, attr_name, &attr) == NULL || dwarf_formsdata(&attr, result) != 0) return -ENOENT; From patchwork Tue Nov 1 13:48:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13672 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2977703wru; Tue, 1 Nov 2022 06:51:24 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6bp35amWfrJoGVGcnhYGhwHaOBXEso7/JbctY3rffqoOwjEj8MusPbYV0kaiY+kTQjUu9a X-Received: by 2002:a05:6402:1c0a:b0:463:3cda:3750 with SMTP id ck10-20020a0564021c0a00b004633cda3750mr13420936edb.341.1667310683954; Tue, 01 Nov 2022 06:51:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667310683; cv=none; d=google.com; s=arc-20160816; b=QzWeOuzY92rUt6AhQLNhFLlsdqWjZTt3dG52p6olXnzuIgid7fqSsX1AOfBEWV1IhC dJ6M2Ew0sTXAAZG7+pbZgPg19XmCrS2sUk0FadXVQL2DnUD/LlME/2XjMU6lUeSbxEhP IP2hmm3A2UD7MGpGuKw/vYEFfnnmYy6CnFpDc5vxo6oNbRqGFtxTy09XOXeKuK2FIBc/ N9FnDuqXlHVyzaO4+GwfISjwA9nFVX7lN+2R/4vDE2d0sK2ObR8e8r/YH+7jN0B5nFiZ Tb83erV+m9TVC/kCu3N/qHaWeIQm3svs1ZdXBS06wazhpJThjCbCaloFA2FjqYRhgTGT ddoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EE7rj2IcY2pczVGVOo7l9qRSjr3ACqfILEHeF4S5jWY=; b=wnxPgJ/+084Jeh+qV+oLxoYG98oFV1PoxBzrdAPP80aMrsThL5e74CyZOUEYiVNWld 8M5tzEGywc+cssChvdRGQBOdVLiBWaag6wFG5fZsNtruqrOaQl47CBKCSlV4ATlySvWR IQ4BOlJtCiz2+QUvzyv1ys5GeWVpBo1MM7FaUH41k0lak6GdEitl2P9aey4Tf1PbG1QO hrWoigBtf8IEm1RtKTS36r5NlGnYg0E42DmPWhSyi7iZYo4xAnG0LeLUe/lPaiADZD4V J0enIKxcuLNblqxViDxa0zG6nubAuVd2i1dzvivPQ4swxRYgar6hv/TDkNsGE8yPAYHG mW+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hwd+Yw8M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f19-20020a056402355300b0045d9ceae669si13246655edd.404.2022.11.01.06.50.59; Tue, 01 Nov 2022 06:51:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hwd+Yw8M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230144AbiKANtP (ORCPT + 99 others); Tue, 1 Nov 2022 09:49:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230129AbiKANs5 (ORCPT ); Tue, 1 Nov 2022 09:48:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4D2264DD; Tue, 1 Nov 2022 06:48:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 5F888B81D62; Tue, 1 Nov 2022 13:48:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54230C433D6; Tue, 1 Nov 2022 13:48:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667310533; bh=7VJv3LGZx07VjYJ8GyNu8FKNv+xMB492/CTyp64BuOk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hwd+Yw8MpOsHmSZ4yG1Ti+9ZcyBR3m7xB/BNx7L4eJ+57fGLTX0XOrowBOXYOA0IE 3XKHk5F1gidLkZgpMSqMlfz7UrDwdCbfekHg4Omt2us97FGGJB7KcRymBY+W1qztng eekNOzJ5FtbvZpAyM3kDb2YUyeqnsXTx4UZBWx7qbGWURD08VU1LJrPjUdRMDFkbZZ 1y6vV9CzRMJOWxVPhFcdeM2WllDY/2PKZM/hZqShYYXU5vJ1DC2NfG8tEBd+B4PZoa oCCp9aMKaxSyqMvU1WQM+9Hm+itF8n39T4Ji2huVNhYOKjbTi6vWfCe+sPbunPSyWx sknxwDr6XsLhQ== From: "Masami Hiramatsu (Google)" To: Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Masami Hiramatsu , Steven Rostedt Subject: [PATCH v2 3/3] tools/perf: Fix to get declared file name from clang DWARF5 Date: Tue, 1 Nov 2022 22:48:49 +0900 Message-Id: <166731052936.2100653.13380621874859467731.stgit@devnote3> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog In-Reply-To: <166731050151.2100653.8202870942871353491.stgit@devnote3> References: <166731050151.2100653.8202870942871353491.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748301967710971815?= X-GMAIL-MSGID: =?utf-8?q?1748301967710971815?= From: Masami Hiramatsu (Google) Fix to get the declared file name even if it uses file index 0 in DWARF5, using custom die_get_decl_file() function. Actually, the DWARF5 standard says file index 0 of the DW_AT_decl_file is invalid(*), but there is a discussion and maybe this will be updated. Anyway, the clang generates such DWARF5 file for the linux kernel. Thus it must be handled. Without this, the perf probe returns an error; $ ./perf probe -k $BIN_PATH/vmlinux -s $SRC_PATH -L vfs_read:10 Debuginfo analysis failed. Error: Failed to show lines. With this, it can handle the case correctly; $ ./perf probe -k $BIN_PATH/vmlinux -s $SRC_PATH -L vfs_read:10 11 ret = rw_verify_area(READ, file, pos, count); 12 if (ret) return ret; (* DWARF5 specification 2.14 says "The value 0 indicates that no source file has been specified.") (** http://wiki.dwarfstd.org/index.php?title=DWARF5_Line_Table_File_Numbers) Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Update description so that it is clang version of DWARF5. --- tools/perf/util/dwarf-aux.c | 47 +++++++++++++++++++++++++++------------- tools/perf/util/dwarf-aux.h | 3 +++ tools/perf/util/probe-finder.c | 14 ++++++------ 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 216fc3d959e8..30b36b525681 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -123,7 +123,7 @@ int cu_find_lineinfo(Dwarf_Die *cu_die, Dwarf_Addr addr, if (die_find_realfunc(cu_die, addr, &die_mem) && die_entrypc(&die_mem, &faddr) == 0 && faddr == addr) { - *fname = dwarf_decl_file(&die_mem); + *fname = die_get_decl_file(&die_mem); dwarf_decl_line(&die_mem, lineno); goto out; } @@ -486,6 +486,19 @@ static int die_get_decl_fileno(Dwarf_Die *pdie) return -ENOENT; } +/* Return the file name by index */ +static const char *die_get_file_name(Dwarf_Die *dw_die, int idx) +{ + Dwarf_Die cu_die; + Dwarf_Files *files; + + if (idx < 0 || !dwarf_diecu(dw_die, &cu_die, NULL, NULL) || + dwarf_getsrcfiles(&cu_die, &files, NULL) != 0) + return NULL; + + return dwarf_filesrc(files, idx, NULL, NULL); +} + /** * die_get_call_file - Get callsite file name of inlined function instance * @in_die: a DIE of an inlined function instance @@ -495,18 +508,22 @@ static int die_get_decl_fileno(Dwarf_Die *pdie) */ const char *die_get_call_file(Dwarf_Die *in_die) { - Dwarf_Die cu_die; - Dwarf_Files *files; - int idx; - - idx = die_get_call_fileno(in_die); - if (idx < 0 || !dwarf_diecu(in_die, &cu_die, NULL, NULL) || - dwarf_getsrcfiles(&cu_die, &files, NULL) != 0) - return NULL; - - return dwarf_filesrc(files, idx, NULL, NULL); + return die_get_file_name(in_die, die_get_call_fileno(in_die)); } +/** + * die_get_decl_file - Find the declared file name of this DIE + * @dw_die: a DIE for something declared. + * + * Get declared file name of @dw_die. + * NOTE: Since some version of clang DWARF5 implementation incorrectly uses + * file index 0 for DW_AT_decl_file, die_get_decl_file() will return NULL for + * such cases. Use this function instead. + */ +const char *die_get_decl_file(Dwarf_Die *dw_die) +{ + return die_get_file_name(dw_die, die_get_decl_fileno(dw_die)); +} /** * die_find_child - Generic DIE search function in DIE tree @@ -790,7 +807,7 @@ static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data) } if (addr) { - fname = dwarf_decl_file(in_die); + fname = die_get_decl_file(in_die); if (fname && dwarf_decl_line(in_die, &lineno) == 0) { lw->retval = lw->callback(fname, lineno, addr, lw->data); if (lw->retval != 0) @@ -818,7 +835,7 @@ static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive, int lineno; /* Handle function declaration line */ - fname = dwarf_decl_file(sp_die); + fname = die_get_decl_file(sp_die); if (fname && dwarf_decl_line(sp_die, &lineno) == 0 && die_entrypc(sp_die, &addr) == 0) { lw.retval = callback(fname, lineno, addr, data); @@ -873,7 +890,7 @@ int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data) if (dwarf_tag(rt_die) != DW_TAG_compile_unit) { cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL); dwarf_decl_line(rt_die, &decl); - decf = dwarf_decl_file(rt_die); + decf = die_get_decl_file(rt_die); if (!decf) { pr_debug2("Failed to get the declared file name of %s\n", dwarf_diename(rt_die)); @@ -928,7 +945,7 @@ int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data) dwarf_decl_line(&die_mem, &inl); if (inl != decl || - decf != dwarf_decl_file(&die_mem)) + decf != die_get_decl_file(&die_mem)) continue; } } diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 7ee0fa19b5c4..7ec8bc1083bb 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -50,6 +50,9 @@ int die_get_call_lineno(Dwarf_Die *in_die); /* Get callsite file name of inlined function instance */ const char *die_get_call_file(Dwarf_Die *in_die); +/* Get declared file name of a DIE */ +const char *die_get_decl_file(Dwarf_Die *dw_die); + /* Get type die */ Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem); diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c index 1aa8fcc41c76..54b49ce85c9f 100644 --- a/tools/perf/util/probe-finder.c +++ b/tools/perf/util/probe-finder.c @@ -763,7 +763,7 @@ static int find_best_scope_cb(Dwarf_Die *fn_die, void *data) /* Skip if declared file name does not match */ if (fsp->file) { - file = dwarf_decl_file(fn_die); + file = die_get_decl_file(fn_die); if (!file || strcmp(fsp->file, file) != 0) return 0; } @@ -1071,7 +1071,7 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) return DWARF_CB_OK; /* Check declared file */ - fname = dwarf_decl_file(sp_die); + fname = die_get_decl_file(sp_die); if (!fname) { pr_warning("A function DIE doesn't have decl_line. Maybe broken DWARF?\n"); return DWARF_CB_OK; @@ -1151,7 +1151,7 @@ static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) return DWARF_CB_OK; if (param->file) { - fname = dwarf_decl_file(param->sp_die); + fname = die_get_decl_file(param->sp_die); if (!fname || strtailcmp(param->file, fname)) return DWARF_CB_OK; } @@ -1750,7 +1750,7 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, u64 addr, goto post; } - fname = dwarf_decl_file(&spdie); + fname = die_get_decl_file(&spdie); if (addr == baseaddr) { /* Function entry - Relative line number is 0 */ lineno = baseline; @@ -1787,7 +1787,7 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, u64 addr, } } /* Verify the lineno and baseline are in a same file */ - tmp = dwarf_decl_file(&spdie); + tmp = die_get_decl_file(&spdie); if (!tmp || (fname && strcmp(tmp, fname) != 0)) lineno = 0; } @@ -1902,13 +1902,13 @@ static int line_range_search_cb(Dwarf_Die *sp_die, void *data) /* Check declared file */ if (lr->file) { - fname = dwarf_decl_file(sp_die); + fname = die_get_decl_file(sp_die); if (!fname || strtailcmp(lr->file, fname)) return DWARF_CB_OK; } if (die_match_name(sp_die, lr->function) && die_is_func_def(sp_die)) { - lf->fname = dwarf_decl_file(sp_die); + lf->fname = die_get_decl_file(sp_die); dwarf_decl_line(sp_die, &lr->offset); pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); lf->lno_s = lr->offset + lr->start;