Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1282450lqs; Sat, 15 Jun 2024 02:21:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWPA5o31CSlcGVpGJtQoGQ1RlAgq7Ni/RDRuMseo24UXq4LdyY1DoQCYd5rnxCd5yD5FRLDaHE/39ZGXlZDg9vgTJb+j/SQezV+zS4U8g== X-Google-Smtp-Source: AGHT+IHnKD0lWXeQSTnB5YrB4rH3fJFPVHg7p2Sq92eF8fPHa6OUIPsOUJAAvvA0lVpTFsW5mH1E X-Received: by 2002:a05:620a:4088:b0:795:4fee:81a8 with SMTP id af79cd13be357-798d25319bfmr661289085a.33.1718443260799; Sat, 15 Jun 2024 02:21:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718443260; cv=pass; d=google.com; s=arc-20160816; b=q8ICJolCrdUGDhiOD0sDAv15Gt6rsdBZk/rimHviWehZAwrphewRiv1QM3rWItel4k KkI2NdGvMYFaarYpb6q3lT0hS2XyKNYpPVuzAJR0aDAfEhG/K3TuXEfNsPPudphNnnnp t+DX+xxPzWIqkpL4+i/sl3cr0SqqUYwSL3ze+tYUvjoQiuCICrmBTLhUSIJj0sBalEl4 peoUz/DJpIsFMyScWcc7jgfFTEiEbBucafuGNXSyzOiqpC64rSIB8rzTwdA349LELdfI vnJdzxCWXwh7Q6H+zpmdy6VbaJ9Ouf4KO0HRa3HVji2yR84rT/boLBiNaZrClParVn8i Ugug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=2dSbmxQuIPWJ8eWN3yc+mACNsktPn3QagGdttEuz+3k=; fh=oqZyDXyMmuwXC127KYP8yZvvclD9htJbfhHWow5HG6Q=; b=JOk/r3OFCo2e9pT/y/kUe1CSpmq8QEnAKE5izdisnzsPygmRHkJ6QikcuRagAhZGLf 9cERoS4q/3rSOLqDGSke5k3ztRQ5xWl882EUADzoXeDf60WfM4izQKn/CGzrX4y0ue9x 3IM2m++dg7yScB3aGHcj6IM+rwXs91fsb9OLxE7u+j4HjjqRuVcFEg0FzvPPMY+Jiwxj ruUfWkbvC9NFbfEBb/vnYN+Gc7X6OKA3S4A3EGxkX38boOA99+YUXwHJdoXVfHcwlTNg WlJ18fT6KjqKI279OcHuoUjZlSjPUSf7a7ISAlZikZOjFhRzLd79CFQYUSHKjkha2U84 XDGA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-215807-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215807-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-798ab8ff9aesi552388885a.381.2024.06.15.02.21.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Jun 2024 02:21:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-215807-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-215807-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215807-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8276B1C2156D for ; Sat, 15 Jun 2024 09:21:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1C3F534CDD; Sat, 15 Jun 2024 09:20:46 +0000 (UTC) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC8672E3E4; Sat, 15 Jun 2024 09:20:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718443245; cv=none; b=NLLdqh/3FNBRZq9lH/tXIMVXWOlEueqewwPFKsSvY27D6+pqcq+8S3DUQ8cm2/fhhplRzkhvNpTbfZIjnhY6rLwUMShE/LP+D6BsvERhQkxOWnMy/WCpZRLHueabE6v3hJtpIY+C2mBI9t+MQIXHHBTuHru79jZd3OuhAsVYRY8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718443245; c=relaxed/simple; bh=I/54TK8Eh32J4IC6wgODBKMZiR0HxmWHWcUWSz/7NgU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rnT3nugF6xg7B2h0y6CNiczPhOOIRxW5JpAyD0TFmg/bRBhomPwj7DJYM6Bz6vEZU/oy0zXZxGNccPvkBuvyTVxT824QTbxmxZW/ntWsLAoEkPS0sniakgTz6WAMjfF2YkRcuAk9hJBPRgKaBj0PVT8rntCvTrLfv7IF+5UKF2c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4W1VsH0CLGzPqv3; Sat, 15 Jun 2024 17:17:07 +0800 (CST) Received: from kwepemd100011.china.huawei.com (unknown [7.221.188.204]) by mail.maildlp.com (Postfix) with ESMTPS id 4F58014011D; Sat, 15 Jun 2024 17:20:35 +0800 (CST) Received: from M910t.huawei.com (10.110.54.157) by kwepemd100011.china.huawei.com (7.221.188.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Sat, 15 Jun 2024 17:20:33 +0800 From: Changbin Du To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Nathan Chancellor CC: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Nick Desaulniers , Bill Wendling , Justin Stitt , , , , Changbin Du Subject: [PATCH v2 2/4] perf: disasm: use build_id_path if fallback failed Date: Sat, 15 Jun 2024 17:20:13 +0800 Message-ID: <20240615092015.1820362-3-changbin.du@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240615092015.1820362-1-changbin.du@huawei.com> References: <20240615092015.1820362-1-changbin.du@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemd100011.china.huawei.com (7.221.188.204) If we can not fallback for special dso (vmlinx and vdso), use the build_id_path found previously. To make change easy, this change first refactors the code by extracting two functions read_buildid_linkname() and fallback_filename(). Signed-off-by: Changbin Du --- tools/perf/util/disasm.c | 121 ++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 46 deletions(-) diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index 442e9802a772..3075daa61916 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -1092,14 +1092,72 @@ int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, s return 0; } -static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) +static int read_buildid_linkname(char *filename, char *linkname, size_t linkname_size) { - char linkname[PATH_MAX]; - char *build_id_filename; char *build_id_path = NULL; char *pos; int len; + build_id_path = strdup(filename); + if (!build_id_path) + return ENOMEM; + + /* + * old style build-id cache has name of XX/XXXXXXX.. while + * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. + * extract the build-id part of dirname in the new style only. + */ + pos = strrchr(build_id_path, '/'); + if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) + dirname(build_id_path); + + len = readlink(build_id_path, linkname, linkname_size); + if (len < 0) { + free(build_id_path); + return -1; + } + + linkname[len] = '\0'; + free(build_id_path); + return 0; +} + +static int fallback_filename(struct dso *dso, char *filename, size_t filename_size) +{ + char filepath[PATH_MAX]; + + /* + * If we don't have build-ids or the build-id file isn't in the + * cache, or is just a kallsyms file, well, lets hope that this + * DSO is the same as when 'perf record' ran. + */ + if ((dso__kernel(dso) || dso__is_vdso(dso)) && dso__long_name(dso)[0] == '/') + snprintf(filepath, sizeof(filepath), "%s", dso__long_name(dso)); + else + __symbol__join_symfs(filepath, sizeof(filepath), dso__long_name(dso)); + + mutex_lock(dso__lock(dso)); + if (access(filepath, R_OK) && errno == ENOENT && dso__nsinfo(dso)) { + char *new_name = dso__filename_with_chroot(dso, filepath); + if (new_name) { + strlcpy(filepath, new_name, sizeof(filepath)); + free(new_name); + } + } + mutex_unlock(dso__lock(dso)); + + if (access(filepath, R_OK) && errno == ENOENT) + return ENOENT; + + snprintf(filename, filename_size, "%s", filepath); + return 0; +} + +static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) +{ + char linkname[PATH_MAX]; + char *build_id_filename; + if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(dso)) return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; @@ -1111,57 +1169,28 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil } else { if (dso__has_build_id(dso)) return ENOMEM; - goto fallback; + return fallback_filename(dso, filename, filename_size); } - build_id_path = strdup(filename); - if (!build_id_path) - return ENOMEM; - - /* - * old style build-id cache has name of XX/XXXXXXX.. while - * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. - * extract the build-id part of dirname in the new style only. - */ - pos = strrchr(build_id_path, '/'); - if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) - dirname(build_id_path); + if (access(filename, R_OK)) + return fallback_filename(dso, filename, filename_size); - if (dso__is_kcore(dso)) + if (dso__is_kcore(dso) || dso__is_vdso(dso)) goto fallback; - len = readlink(build_id_path, linkname, sizeof(linkname) - 1); - if (len < 0) - goto fallback; + if (!read_buildid_linkname(filename, linkname, sizeof(linkname) - 1) && + (!strstr(linkname, DSO__NAME_KALLSYMS) && !strstr(linkname, DSO__NAME_VDSO))) { + /* not kallsysms or vdso, use build_id path found above */ + goto out; + } - linkname[len] = '\0'; - if (strstr(linkname, DSO__NAME_KALLSYMS) || strstr(linkname, DSO__NAME_VDSO) || - access(filename, R_OK)) { fallback: - /* - * If we don't have build-ids or the build-id file isn't in the - * cache, or is just a kallsyms file, well, lets hope that this - * DSO is the same as when 'perf record' ran. - */ - if ((dso__kernel(dso) || dso__is_vdso(dso)) && dso__long_name(dso)[0] == '/') - snprintf(filename, filename_size, "%s", dso__long_name(dso)); - else - __symbol__join_symfs(filename, filename_size, dso__long_name(dso)); - - mutex_lock(dso__lock(dso)); - if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) { - char *new_name = dso__filename_with_chroot(dso, filename); - if (new_name) { - strlcpy(filename, new_name, filename_size); - free(new_name); - } - } - mutex_unlock(dso__lock(dso)); - } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { - dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); + if (fallback_filename(dso, filename, filename_size)) { + /* if fallback failed, use build_id path found above */ +out: + if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) + dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); } - - free(build_id_path); return 0; } -- 2.34.1