From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 0C13EA0093; Thu, 13 Oct 2022 15:41:45 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E3D4942F7F; Thu, 13 Oct 2022 15:41:44 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 87F4142EAF for ; Thu, 13 Oct 2022 15:41:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665668503; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AsaZTF9W4GyFLPaKaP2T3rlO5oH3Ap4YW3g4AIIxtkw=; b=YZg5iXXHsK1awxPMUmjS2gN5WJI9UxSQLKnrT6LkzIh7c5ILS5/S45rHcbjuTe/KHVbdST /96j4poiL2eoqqiiyhITnR0sstAGUzwL+KnveZCboBHhGw2yLe2VXIE1IgU1OeZZoMFmrN jqiIZErjFr9GwLtamkXP7Hc6vrEEtqE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-643-JCskru2YOR-POuq2Jfn3Vw-1; Thu, 13 Oct 2022 09:41:41 -0400 X-MC-Unique: JCskru2YOR-POuq2Jfn3Vw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0F5092932498; Thu, 13 Oct 2022 13:41:40 +0000 (UTC) Received: from marty.home (unknown [10.39.208.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1ED6A40369AB; Thu, 13 Oct 2022 13:41:38 +0000 (UTC) From: Robin Jarry To: dev@dpdk.org Cc: Robin Jarry , Olivier Matz Subject: [PATCH] usertools/pmdinfo: remove dependency to ldd Date: Thu, 13 Oct 2022 15:41:25 +0200 Message-Id: <20221013134125.448437-1-rjarry@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Some environments (buildroot) do not have the ldd utility installed by default. However, ldd is often only a wrapper shell script that actually checks that the arguments are valid ELF files and executes them with the LD_TRACE_LOADED_OBJECTS=1 variable set in the environment. Since ld.so is the actual ELF interpreter which is loaded first when executing a program, executing any dynamic ELF program/library with that variable set will cause all dependent dynamic libraries to be printed and ld.so will exit before even running main. Excerpt from ld.so(7) man page: LD_TRACE_LOADED_OBJECTS If set (to any value), causes the program to list its dynamic dependencies, as if run by ldd(1), instead of running normally. Change dpdk-pmdinfo.py to actually "execute" the files provided on the command line with LD_TRACE_LOADED_OBJECTS=1 set. Ensure that the files are valid dynamically executable ELF programs to avoid obscure and confusing errors. Reported-by: Olivier Matz Signed-off-by: Robin Jarry --- doc/guides/tools/pmdinfo.rst | 4 ++-- usertools/dpdk-pmdinfo.py | 34 +++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/doc/guides/tools/pmdinfo.rst b/doc/guides/tools/pmdinfo.rst index a9217de4eef2..1406b9c442eb 100644 --- a/doc/guides/tools/pmdinfo.rst +++ b/doc/guides/tools/pmdinfo.rst @@ -37,8 +37,8 @@ Arguments .. option:: ELF_FILE - DPDK application binary or dynamic library. - Any linked ``librte_*.so`` library (as reported by ``ldd``) will also be analyzed. + Executable DPDK application binary or dynamic library. + Any linked ``librte_*.so`` library (as reported by ``ld.so``) will also be analyzed. Can be specified multiple times. Environment Variables diff --git a/usertools/dpdk-pmdinfo.py b/usertools/dpdk-pmdinfo.py index 67d023a04711..01bb90666bcc 100755 --- a/usertools/dpdk-pmdinfo.py +++ b/usertools/dpdk-pmdinfo.py @@ -97,9 +97,9 @@ def parse_args() -> argparse.Namespace: "elf_files", metavar="ELF_FILE", nargs="+", - type=existing_file, + type=executable_elf_file, help=""" - DPDK application binary or dynamic library. + Executable DPDK application binary or dynamic library. """, ) return parser.parse_args() @@ -180,14 +180,24 @@ def get_plugin_libs(binaries: Iterable[Path]) -> Iterator[Path]: # ---------------------------------------------------------------------------- -def existing_file(value: str) -> Path: +def executable_elf_file(value: str) -> Path: """ - Argparse type= callback to ensure an argument points to a valid file path. + Argparse type= callback to ensure an argument points to a valid ELF file + path which can be executed. """ - path = Path(value) - if not path.is_file(): - raise argparse.ArgumentTypeError(f"{value}: No such file") - return path + try: + with open(value, "rb") as f: + elf = ELFFile(f) + if elf.header.e_type not in ("ET_DYN", "ET_EXEC"): + raise ELFError(f"unknown type: {elf.header.e_type!r}") + if not os.access(value, os.X_OK): + raise OSError("is not executable") + except ELFError as e: + raise argparse.ArgumentTypeError(f"{value}: invalid ELF: {e}") from e + except OSError as e: + raise argparse.ArgumentTypeError(f"{value}: {e}") from e + + return Path(value) # ---------------------------------------------------------------------------- @@ -270,7 +280,7 @@ def get_elf_strings(path: Path, section: str, prefix: str) -> Iterator[str]: # ---------------------------------------------------------------------------- -LDD_LIB_RE = re.compile( +LOADED_OBJECT_RE = re.compile( r""" ^ # beginning of line \t # tab @@ -290,14 +300,16 @@ def get_needed_libs(path: Path) -> Iterator[Path]: """ Extract the dynamic library dependencies from an ELF executable. """ + env = os.environ.copy() + env["LD_TRACE_LOADED_OBJECTS"] = "1" with subprocess.Popen( - ["ldd", str(path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE + [str(path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env ) as proc: out, err = proc.communicate() if proc.returncode != 0: err = err.decode("utf-8").splitlines()[-1].strip() raise Exception(f"cannot read ELF file: {err}") - for match in LDD_LIB_RE.finditer(out.decode("utf-8")): + for match in LOADED_OBJECT_RE.finditer(out.decode("utf-8")): libname, libpath = match.groups() if libname.startswith("librte_"): libpath = Path(libpath) -- 2.37.3