From patchwork Sat Sep 16 10:18:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 76229 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 381943858002 for ; Sat, 16 Sep 2023 10:18:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 381943858002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1694859538; bh=SHs0PuauLcYlKtbf6GxlYSwJ5CduA/i96XlaHhHIhDg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=TkjqPEVSlCtbfk/FmZOJw/0XuuTiKCSkRVHa9xlY164KTVrsn+tyfBzIP5CNMhyHE I++HR/wxL64v2ydedtJXMuEVxTacYHIsMaE/MOTV5mLYRHwP4HSRVMrcYMewx4/Xwi xUMkklfMX0e5emM3b6fvWAGGHfPS5gLauMr+xSoo= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id EFB893857B9B for ; Sat, 16 Sep 2023 10:18:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EFB893857B9B Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-158-fTTrnYcEPqW8VNs5QpwjpQ-1; Sat, 16 Sep 2023 06:18:24 -0400 X-MC-Unique: fTTrnYcEPqW8VNs5QpwjpQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f42bcef2acso23813905e9.2 for ; Sat, 16 Sep 2023 03:18:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694859503; x=1695464303; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SHs0PuauLcYlKtbf6GxlYSwJ5CduA/i96XlaHhHIhDg=; b=rfW2FFWHUsuQqIGi2ZWcbmpLAsA6lrffUJE/e94L4Lo//jRAbpW7EPpOa8Fb/THppv 0z5lXcv9V5YpoQ3zdkAr4G6QY/3haP/NYqIwL4AX0GiCmLHy9YfK1h+jffbmmwXH1rsx FPoEiwg0cMMFIypEw03XNXMXpLOac11Er6LC+c5wFBGcIg3Q5v96/VYYG6XA0s9zegK3 hOuzB31PP0Uzjhno1vbPZ/Qgdz9aSCt1DTGhJ2c06zEe/eVxUOnu6bP2Trzhmo3iqq1l YD91Cg3Tnecn406L19OZkLy4v3Ay7ToaYLH8Vrk/EjtmUZ6XqRN0Yh57Y+jdbf/ETm1G wSlg== X-Gm-Message-State: AOJu0Yz37jGqENLLoeVuJk1QCyl79N+3K4CgM50CrU6FUys0bfGXpGLU 1ZEH5iZk/6jGmp/wM0DsoHEtKbkTnSbDJVFIn1kTcFC4i9LEx0BO6fliWSCGbry7XHA5nAUTOSU ylyz/v2oBXQvVUqyV71AMzDXI6exDOaeRIBZ8oH5Stble7LxmElz7kmG5UnR+tJwcsEtCwGB5y6 3qTS5cnQ== X-Received: by 2002:a05:600c:221a:b0:3fa:97ad:2ba5 with SMTP id z26-20020a05600c221a00b003fa97ad2ba5mr3731980wml.31.1694859502655; Sat, 16 Sep 2023 03:18:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHadpTBRJiJjveeDajw3jUqK/f4ToZk6Pc6nBp76XODQPrNCIO/NQCmzN8rn1KSWDrkEZvMUw== X-Received: by 2002:a05:600c:221a:b0:3fa:97ad:2ba5 with SMTP id z26-20020a05600c221a00b003fa97ad2ba5mr3731963wml.31.1694859502216; Sat, 16 Sep 2023 03:18:22 -0700 (PDT) Received: from localhost (92.40.218.107.threembb.co.uk. [92.40.218.107]) by smtp.gmail.com with ESMTPSA id z25-20020a1c4c19000000b003fe29f6b61bsm6879672wmf.46.2023.09.16.03.18.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Sep 2023 03:18:21 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH 3/9] gdb/python: new Progspace.executable_filename attribute Date: Sat, 16 Sep 2023 11:18:04 +0100 Message-Id: <4c77a079079814ae5d476b5fc61b2e25539bd37f.1694858967.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Add a new Progspace.executable_filename attribute that contains the path to the executable for this program space, or None if no executable is set. The path within this attribute will be set by the "exec-file" and/or "file" commands. Accessing this attribute for an invalid program space will raise an exception. This new attribute is similar too, but not the same as the existing gdb.Progspace.filename attribute. If I could change the past, I'd change the 'filename' attribute to 'symbol_filename', which is what it actually represents. The old attribute will be set by the 'symbol-file' command, while the new attribute is set by the 'exec-file' command. Obviously the 'file' command sets both of these attributes. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 6 ++ gdb/doc/python.texi | 15 ++++ gdb/python/py-progspace.c | 21 +++++ gdb/testsuite/gdb.python/py-exec-file.c | 22 +++++ gdb/testsuite/gdb.python/py-exec-file.exp | 100 ++++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 gdb/testsuite/gdb.python/py-exec-file.c create mode 100644 gdb/testsuite/gdb.python/py-exec-file.exp diff --git a/gdb/NEWS b/gdb/NEWS index f2f5dabb287..93bc9c6a2c0 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -291,6 +291,12 @@ info main Progspace.filename is not None), otherwise, this attribute is itself None. + ** New attribute Progspace.executable_filename. This attribute + holds a string containing a path set by the "exec-file" or "file" + commands, or None if no executable file is set. This isn't the + exact string passed by the user to these commands; the path will + have been partially resolved to an absolute path. + *** 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 03299cc3cf7..206cf6b4e18 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -5059,6 +5059,21 @@ access this attribute will raise a @code{RuntimeError} exception. @end defvar +@defvar Progspace.executable_filename +The file name, as a string, of the executable file in use by this +program space. The executable file is the file that @value{GDBN} will +invoke in order to start an inferior when using a native target. The +file name within this attribute is updated by the @kbd{exec-file} and +@kbd{file} commands. + +If no executable is currently set within this @code{Progspace} then +this attribute contains @code{None}. + +If the @code{Progspace} is invalid, i.e.@:, when +@code{Progspace.is_valid()} returns @code{False}, then attempting to +access this attribute will raise a @code{RuntimeError} exception. +@end defvar + @defvar Progspace.pretty_printers The @code{pretty_printers} attribute is a list of functions. It is used to look up pretty-printers. A @code{Value} is passed to each diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 929c5f4fa70..1319978d34a 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -131,6 +131,25 @@ pspy_get_symbol_file (PyObject *self, void *closure) Py_RETURN_NONE; } +/* Implement the gdb.Progspace.executable_filename attribute. Retun a + string containing the name of the current executable, or None if no + executable is currently set. If the Progspace is invalid then raise an + exception. */ + +static PyObject * +pspy_get_exec_file (PyObject *self, void *closure) +{ + pspace_object *obj = (pspace_object *) self; + + PSPY_REQUIRE_VALID (obj); + + const char *filename = obj->pspace->exec_filename.get (); + if (filename != nullptr) + return host_string_to_python_string (filename).release (); + + Py_RETURN_NONE; +} + static void pspy_dealloc (PyObject *self) { @@ -596,6 +615,8 @@ static gdb_PyGetSetDef pspace_getset[] = { "symbol_file", pspy_get_symbol_file, nullptr, "The gdb.Objfile for the progspace's main symbol file, or None.", nullptr}, + { "executable_filename", pspy_get_exec_file, nullptr, + "The filename for the progspace's executable, or None.", nullptr}, { "pretty_printers", pspy_get_printers, pspy_set_printers, "Pretty printers.", NULL }, { "frame_filters", pspy_get_frame_filters, pspy_set_frame_filters, diff --git a/gdb/testsuite/gdb.python/py-exec-file.c b/gdb/testsuite/gdb.python/py-exec-file.c new file mode 100644 index 00000000000..8cbea3b6892 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-exec-file.c @@ -0,0 +1,22 @@ +/* This test program is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.python/py-exec-file.exp b/gdb/testsuite/gdb.python/py-exec-file.exp new file mode 100644 index 00000000000..14e5088af1c --- /dev/null +++ b/gdb/testsuite/gdb.python/py-exec-file.exp @@ -0,0 +1,100 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +require allow_python_tests + +load_lib gdb-python.exp + +standard_testfile + +set binfile1 ${binfile}-a +set binfile2 ${binfile}-b + +if {[build_executable "failed to prepare first executable" \ + $binfile1 $srcfile]} { + return -1 +} + +if {[build_executable "failed to prepare second executable" \ + $binfile2 $srcfile]} { + return -1 +} + +set binfile1 [gdb_remote_download host $binfile1] +set binfile2 [gdb_remote_download host $binfile2] + +# Check that the executable_filename is set correctly after using the +# 'file' command. +with_test_prefix "using 'file' command" { + clean_restart + + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "None" \ + "check executable_filename when no file is loaded" + + gdb_test "file $binfile1" \ + "Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \ + "load first executable" + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "[string_to_regexp $binfile1]" \ + "check executable_filename when first executable is loaded" + + gdb_test "file $binfile2" \ + "Reading symbols from [string_to_regexp $binfile2]\\.\\.\\..*" \ + "load second executable" \ + "Load new symbol table from .*\? .y or n. " "y" + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "[string_to_regexp $binfile2]" \ + "check executable_filename when second executable is loaded" + + gdb_unload + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "None" \ + "check executable_filename after unloading file" +} + +# Check that the executable_filename is correctly set when we only set +# the exec-file. +with_test_prefix "using 'exec-file' command" { + clean_restart + gdb_test_no_output "exec-file $binfile1" \ + "load first executable" + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "[string_to_regexp $binfile1]" \ + "check executable_filename when first executable is loaded" + + gdb_test_no_output "exec-file $binfile2" \ + "load second executable" + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "[string_to_regexp $binfile2]" \ + "check executable_filename when second executable is loaded" + + gdb_test "exec-file" "No executable file now\\." + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "None" \ + "check executable_filename after unloading file" +} + +# Check that setting the symbol-file doesn't cause the +# executable_filename to be set. +with_test_prefix "using 'symbol-file' command" { + clean_restart + gdb_test "symbol-file $binfile1" \ + "Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \ + "load first executable" + gdb_test "python print(gdb.current_progspace().executable_filename)" \ + "None" \ + "check executable_filename after setting symbol-file" +}