From patchwork Tue Jul 11 14:48:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72495 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A0F493858C62 for ; Tue, 11 Jul 2023 14:49:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0F493858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689086964; bh=163fFEsh1MRf2o1SsTv8TJC0zwAdqQk7DGcltRIjcaE=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=mEx5wFvMBQvOzsrnSSLVsgFFiF5vHhvXyFticAGmhcYmNMng2FsUD0JE8eL4xk3zA 00hf6s7xA3Zc11ZkKbWGyETTZqUbD3BD748j7K8FCqhvVh7yfLjP/BnQ/nqK6cjphu zjvAwcK9L7HFMF80DLQ+XKEfb+A45//2POLjrJQs= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by sourceware.org (Postfix) with ESMTPS id 5A1F23858C1F for ; Tue, 11 Jul 2023 14:48:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A1F23858C1F Received: by mail-io1-xd34.google.com with SMTP id ca18e2360f4ac-7835ae70e46so186465839f.3 for ; Tue, 11 Jul 2023 07:48:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689086935; x=1691678935; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=163fFEsh1MRf2o1SsTv8TJC0zwAdqQk7DGcltRIjcaE=; b=ZQTqAoDz3NVxPQ49T6guyU/u5L2T9f27FZt4pr8uM/Byh1/gQWxqFZaeQBW0BmTVD1 ew+pQWQeiklLiKAzaAz2PLeo2WVAX+ZeYkUmUieelznNxjGtnZeoYtUfAXMjfSvrnMTj ursERqqQqqe2O6pQKnGdh6a94iRz9kZoE0wq/v+LK9J+RN+/PMWWwBnoiuzrcyP3oCAU ry0JMgHKPhaNq9ccD9Ks/f8Ood4JWwfVQ2lxtMCIZOy6y8h11fESQ0bPt/4Vg4EZnHKf 8r0Clvm0b6BqEGrtTJR9LPkk/STOAHmT33lEJ+FqEdQa5bi8ycbzJIyskDhRWhTu7VU6 W7KQ== X-Gm-Message-State: ABy/qLaaholSfpT4FJkCoRrCAvgfpWayeejXCf9jQr0pfwE4zabKt6ez EFEVTNwNcCEjL8+xjPGNaAiVopjzn5LwBGOnKoHNoA== X-Google-Smtp-Source: APBJJlEc2ImjkrftOBigH+iQ/DPdUD/ypl03FBzimApcv0RU/jIkBTmcRky8GS9ORy5nqhkDhdOGRw== X-Received: by 2002:a5d:9f0f:0:b0:786:25a3:ef30 with SMTP id q15-20020a5d9f0f000000b0078625a3ef30mr16063307iot.7.1689086935575; Tue, 11 Jul 2023 07:48:55 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id r20-20020a056638101400b0041f4da30787sm567226jab.167.2023.07.11.07.48.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 07:48:55 -0700 (PDT) Date: Tue, 11 Jul 2023 08:48:53 -0600 Subject: [PATCH 2/3] Add Progspace.objfile_for_address MIME-Version: 1.0 Message-Id: <20230711-dap-modules-v1-2-b4f6067c1b2e@adacore.com> References: <20230711-dap-modules-v1-0-b4f6067c1b2e@adacore.com> In-Reply-To: <20230711-dap-modules-v1-0-b4f6067c1b2e@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This adds a new objfile_for_address method to gdb.Progspace. This makes it easy to find the objfile for a given address. There's a related PR; and while this change would have been sufficient for my original need, it's not clear to me whether I should close the bug. Nevertheless I think it makes sense to at least mention it here. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19288 Reviewed-By: Eli Zaretskii --- gdb/NEWS | 3 +++ gdb/doc/python.texi | 6 ++++++ gdb/progspace.c | 16 ++++++++++++++++ gdb/progspace.h | 4 ++++ gdb/python/py-progspace.c | 27 +++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-progspace.exp | 7 +++++++ 6 files changed, 63 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index b924834d3d7..a97b5fb9364 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -230,6 +230,9 @@ info main ** gdb.Value now has the 'assign' method. + ** gdb.Progspace now has the new method "objfile_for_address". This + returns the gdb.Objfile, if any, that covers a given address. + *** Changes in GDB 13 * MI version 1 is deprecated, and will be removed in GDB 14. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 9a342f34bf0..9bb907d1982 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -5068,6 +5068,12 @@ Return the name of the shared library holding the given @var{address} as a string, or @code{None}. @end defun +@findex Progspace.objfile_for_address +@defun Progspace.objfile_for_address (address) +Return the @code{gdb.Objfile} holding the given address, or +@code{None} if no objfile covers it. +@end defun + One may add arbitrary attributes to @code{gdb.Progspace} objects in the usual Python way. This is useful if, for example, one needs to do some extra record keeping diff --git a/gdb/progspace.c b/gdb/progspace.c index 32bdfebcf7c..5cf8334ee67 100644 --- a/gdb/progspace.c +++ b/gdb/progspace.c @@ -180,6 +180,22 @@ program_space::remove_objfile (struct objfile *objfile) /* See progspace.h. */ +struct objfile * +program_space::objfile_for_address (CORE_ADDR address) +{ + for (auto iter : objfiles ()) + { + /* Don't check separate debug objfiles. */ + if (iter->separate_debug_objfile_backlink != nullptr) + continue; + if (is_addr_in_objfile (address, iter)) + return iter; + } + return nullptr; +} + +/* See progspace.h. */ + void program_space::exec_close () { diff --git a/gdb/progspace.h b/gdb/progspace.h index 85215f0e2f1..ee12d89c173 100644 --- a/gdb/progspace.h +++ b/gdb/progspace.h @@ -249,6 +249,10 @@ struct program_space /* Free all the objfiles associated with this program space. */ void free_all_objfiles (); + /* Return the objfile containing ADDRESS, or nullptr if the address + is outside all objfiles in this progspace. */ + struct objfile *objfile_for_address (CORE_ADDR address); + /* Return a range adapter for iterating over all the solibs in this program space. Use it like: diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index a231d240342..b98ac8dde61 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -392,6 +392,30 @@ pspy_solib_name (PyObject *o, PyObject *args) return host_string_to_python_string (soname).release (); } +/* Implement objfile_for_address. */ + +static PyObject * +pspy_objfile_for_address (PyObject *o, PyObject *args) +{ + CORE_ADDR addr; + PyObject *addr_obj; + + pspace_object *self = (pspace_object *) o; + + PSPY_REQUIRE_VALID (self); + + if (!PyArg_ParseTuple (args, "O", &addr_obj)) + return nullptr; + if (get_addr_from_python (addr_obj, &addr) < 0) + return nullptr; + + struct objfile *objf = self->pspace->objfile_for_address (addr); + if (objf == nullptr) + Py_RETURN_NONE; + + return objfile_to_objfile_object (objf).release (); +} + /* Return the innermost lexical block containing the specified pc value, or 0 if there is none. */ static PyObject * @@ -569,6 +593,9 @@ static PyMethodDef progspace_object_methods[] = { "solib_name", pspy_solib_name, METH_VARARGS, "solib_name (Long) -> String.\n\ Return the name of the shared library holding a given address, or None." }, + { "objfile_for_address", pspy_objfile_for_address, METH_VARARGS, + "objfile_for_address (int) -> gdb.Objfile\n\ +Return the objfile containing the given address, or None." }, { "block_for_pc", pspy_block_for_pc, METH_VARARGS, "Return the block containing the given pc value, or None." }, { "find_pc_line", pspy_find_pc_line, METH_VARARGS, diff --git a/gdb/testsuite/gdb.python/py-progspace.exp b/gdb/testsuite/gdb.python/py-progspace.exp index 638b27927c6..f0dc208ae4b 100644 --- a/gdb/testsuite/gdb.python/py-progspace.exp +++ b/gdb/testsuite/gdb.python/py-progspace.exp @@ -68,6 +68,13 @@ if ![is_address_zero_readable] { gdb_test "python print (gdb.current_progspace ().block_for_pc (0))" "None" } +gdb_test "python print(gdb.current_progspace().objfile_for_address(${pc_val}).username)" \ + ".*py-progspace" \ + "objfile for pc" +gdb_test "python print(gdb.current_progspace().objfile_for_address(0))" \ + "None" \ + "no objfile for 0" + # With a single inferior, progspace.objfiles () and gdb.objfiles () should # be identical. gdb_test "python print (progspace.objfiles () == gdb.objfiles ())" "True"