From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id BB52C3856962 for ; Sat, 16 Sep 2023 10:18:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB52C3856962 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694859516; 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: in-reply-to:in-reply-to:references:references; bh=H8Ri08Q8lvZ0EdIsUBsyrkJYNqV1tkvFV1sZcwKxmSM=; b=Oy5tQU5ixqBhUHCfMFJhk18BewqLweHKLpHLFv138eOQYpbwkEkFwHwn6hT+sPS8J05YqT bAqDBboajSoUBF6RGKLkYqMZ+n/J01M4u6gB5JcRiTysDGelfOAEHI9P8gXy55MyNq25lv 3SYnRcenMvIFU/qIOsUK68MctYBuO7k= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-bc_EB7alPDuAz-uXnfAD9g-1; Sat, 16 Sep 2023 06:18:34 -0400 X-MC-Unique: bc_EB7alPDuAz-uXnfAD9g-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-3fef5403093so13805615e9.0 for ; Sat, 16 Sep 2023 03:18:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694859513; x=1695464313; 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=H8Ri08Q8lvZ0EdIsUBsyrkJYNqV1tkvFV1sZcwKxmSM=; b=j8LvL0XFJLkWYRRuFVRNgzdQ/7jS/KmobfZDoy2R+3+KQatn+4zfDcFGgTwZ6LskNT 6FAbRrf/7DNWwD0bI5PtKrp+ZUGRm2/i6oFTk8y+AuszS7lVcnjeYrFgg7GaSMkod3Vd q+ASfMrqE/WkQGXJVolcMZ4K9f4Bt8TUWo/Wcs5yWAu2LhaBVNn2iBsHf1Z2Dg7wkkqQ PV5D7Of8yYvstnSi9W++dwt45fec5PBJZJSHPVVD3GOOKs5mKiaZHTBsLKmZ2YjxGlco 9jU05AvDNe/ePTiVElRIAXnzGSuVMB92g+cZ9ynEPCe5ZiymE1ep5TY0hChgDi5X0ByJ RcCA== X-Gm-Message-State: AOJu0YwHd3oMp/25oDNAg9V9LCWXtI9tQj3c1SraHy0ZSU0iBku+ECS9 Jj9YKLS8cxps2p3+QZ1lJtWhFLTVNatK61OnmpeX52cwtV2HS10JafkOeOk2yKfZDL1YayXh4zz 8B2f+eicF05SNRJq5i4cpQaN2VxdXDqnNvVW14HRpBRWE7kEElAsL1cCSAQHPuwbkpFy5P1qikg +81NdpeA== X-Received: by 2002:a7b:c857:0:b0:401:b425:2414 with SMTP id c23-20020a7bc857000000b00401b4252414mr5589971wml.18.1694859513252; Sat, 16 Sep 2023 03:18:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFg3tlFeWMkl+dRSj2Dv+iyv7lt8dCuyFMrBGWYHlBX8qZW6F84Aqz1LUhxz5TOuSRGjvFUpw== X-Received: by 2002:a7b:c857:0:b0:401:b425:2414 with SMTP id c23-20020a7bc857000000b00401b4252414mr5589953wml.18.1694859512824; Sat, 16 Sep 2023 03:18:32 -0700 (PDT) Received: from localhost (92.40.218.107.threembb.co.uk. [92.40.218.107]) by smtp.gmail.com with ESMTPSA id k16-20020a05600c1c9000b004047f3b73f9sm3445886wms.21.2023.09.16.03.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Sep 2023 03:18:32 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 8/9] gdb/python: make the executable_changed event available from Python Date: Sat, 16 Sep 2023 11:18:09 +0100 Message-Id: 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,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 List-Id: This commit makes the executable_changed observable available through the Python API as an event. There's nothing particularly interesting going on here, it just follows the same pattern as many of the other Python events we support. The new event registry is called events.executable_changed, and this emits an ExecutableChangedEvent object which has two attributes, a gdb.Progspace called 'progspace', this is the program space in which the executable changed, and a Boolean called 'reload', which is True if the same executable changed on disk and has been reloaded, or is False when a new executable has been loaded. One interesting thing did come up during testing though, you'll notice the test contains a setup_kfail call. During testing I observed that the executable_changed event would trigger twice when GDB restarted an inferior. However, the ExecutableChangedEvent object is identical for both calls, so the wrong information is never sent out, we just see one too many events. I tracked this down to how the reload_symbols function (symfile.c) takes care to also reload the executable, however, I've split fixing this into a separate commit, so see the next commit for details. --- gdb/NEWS | 5 ++ gdb/doc/python.texi | 32 +++++++ gdb/python/py-all-events.def | 1 + gdb/python/py-event-types.def | 5 ++ gdb/python/py-progspace.c | 54 ++++++++++++ gdb/testsuite/gdb.python/py-exec-file.exp | 100 ++++++++++++++++++++++ 6 files changed, 197 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index 93bc9c6a2c0..10975dbb27b 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -297,6 +297,11 @@ info main exact string passed by the user to these commands; the path will have been partially resolved to an absolute path. + ** A new executable_changed event registry is available. This event + emits ExecutableChangedEvent objects, which have 'progspace' (a + gdb.Progspace) and 'reload' (a Boolean) attributes. This event + is emitted when gdb.Progspace.executable_filename changes. + *** 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 206cf6b4e18..9f7c7cb076a 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -3893,6 +3893,38 @@ The @code{gdb.TargetConnection} that is being removed. @end defvar +@item events.executable_changed +Emit @code{gdb.ExecutableChangedEvent} which indicates that the +@code{gdb.Progspace.executable_filename} has changed. + +Changed can mean that either, the value in +@code{gdb.Progspace.executable_filename} has changed to a new path, or +the executable pointed to by @code{gdb.Progspace.executable_filename} +has changed on disk, and @value{GDBN} has reloaded it. + +@defvar ExecutableChangedEvent.progspace +The @code{gdb.Progspace} in which the current executable has changed. +The path to the updated executable will be visible in +@code{gdb.Progspace.executable_filename} (@pxref{Progspaces In Python}). +@end defvar +@defvar ExecutableChangedEvent.reload +This attribute will be @code{True} if the value of +@code{gdb.Progspace.executable_filename} didn't change, but the file +pointed to instead changed on disk, and @value{GDBN} reloaded it. + +When this attribute is @code{False}, the value in +@code{gdb.Progspace.executable_filename} was changed to point to a new +file. +@end defvar + +Remember that @value{GDBN} tracks the executable file, and the symbol +file separately, these are visible as +@code{gdb.Progspace.executable_filename} and +@code{gdb.Progspace.filename} respectively. When using the @kbd{file} +command, @value{GDBN} updates both of these fields, but the executable +file is updated first, so when this event is emitted, the executable +filename will have changed, but the symbol filename might still hold +its previous value. @end table @node Threads In Python diff --git a/gdb/python/py-all-events.def b/gdb/python/py-all-events.def index aa28f2c6f8b..04a12e1bdf0 100644 --- a/gdb/python/py-all-events.def +++ b/gdb/python/py-all-events.def @@ -42,3 +42,4 @@ GDB_PY_DEFINE_EVENT(breakpoint_modified) GDB_PY_DEFINE_EVENT(before_prompt) GDB_PY_DEFINE_EVENT(gdb_exiting) GDB_PY_DEFINE_EVENT(connection_removed) +GDB_PY_DEFINE_EVENT(executable_changed) diff --git a/gdb/python/py-event-types.def b/gdb/python/py-event-types.def index 395d6c07447..b862094650d 100644 --- a/gdb/python/py-event-types.def +++ b/gdb/python/py-event-types.def @@ -125,3 +125,8 @@ GDB_PY_DEFINE_EVENT_TYPE (connection, "ConnectionEvent", "GDB connection added or removed object", event_object_type); + +GDB_PY_DEFINE_EVENT_TYPE (executable_changed, + "ExecutableChangedEvent", + "GDB executable changed event", + event_object_type); diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 1319978d34a..082509b2b5b 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -26,6 +26,8 @@ #include "arch-utils.h" #include "solib.h" #include "block.h" +#include "py-event.h" +#include "observable.h" struct pspace_object { @@ -592,9 +594,61 @@ gdbpy_is_progspace (PyObject *obj) return PyObject_TypeCheck (obj, &pspace_object_type); } +/* Emit an ExecutableChangedEvent event to REGISTRY. Return 0 on success, + or a negative value on error. PSPACE is the program_space in which the + current executable has changed, and RELOAD_P is true if the executable + path stayed the same, but the file on disk changed, or false if the + executable path actually changed. */ + +static int +emit_executable_changed_event (eventregistry_object *registry, + struct program_space *pspace, bool reload_p) +{ + gdbpy_ref<> event_obj + = create_event_object (&executable_changed_event_object_type); + if (event_obj == nullptr) + return -1; + + gdbpy_ref<> py_pspace = pspace_to_pspace_object (pspace); + if (py_pspace == nullptr + || evpy_add_attribute (event_obj.get (), "progspace", + py_pspace.get ()) < 0) + return -1; + + gdbpy_ref<> py_reload_p (PyBool_FromLong (reload_p ? 1 : 0)); + if (py_reload_p == nullptr + || evpy_add_attribute (event_obj.get (), "reload", + py_reload_p.get ()) < 0) + return -1; + + return evpy_emit_event (event_obj.get (), registry); +} + +/* Listener for the executable_changed observable, this is called when the + current executable within PSPACE changes. RELOAD_P is true if the + executable path stayed the same but the file changed on disk. RELOAD_P + is false if the executable path was changed. */ + +static void +gdbpy_executable_changed (struct program_space *pspace, bool reload_p) +{ + if (!gdb_python_initialized) + return; + + gdbpy_enter enter_py; + + if (!evregpy_no_listeners_p (gdb_py_events.executable_changed)) + if (emit_executable_changed_event (gdb_py_events.executable_changed, + pspace, reload_p) < 0) + gdbpy_print_stack (); +} + static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_pspace (void) { + gdb::observers::executable_changed.attach (gdbpy_executable_changed, + "py-progspace"); + if (PyType_Ready (&pspace_object_type) < 0) return -1; diff --git a/gdb/testsuite/gdb.python/py-exec-file.exp b/gdb/testsuite/gdb.python/py-exec-file.exp index 14e5088af1c..5ad3cd7e50f 100644 --- a/gdb/testsuite/gdb.python/py-exec-file.exp +++ b/gdb/testsuite/gdb.python/py-exec-file.exp @@ -35,11 +35,59 @@ if {[build_executable "failed to prepare second executable" \ set binfile1 [gdb_remote_download host $binfile1] set binfile2 [gdb_remote_download host $binfile2] +# Setup a Python function to listen for the executable changed event. +proc setup_exec_change_handler {} { + gdb_py_test_silent_cmd \ + [multi_line \ + "python" \ + "def reset_state():" \ + " global exec_changed_state" \ + " exec_changed_state = \[0, None, None\]" \ + "end" ] \ + "build reset_state function" 0 + + gdb_py_test_silent_cmd \ + [multi_line \ + "python" \ + "def executable_changed(event):" \ + " global exec_changed_state" \ + " exec_changed_state\[0\] += 1" \ + " exec_changed_state\[1\] = event.progspace.executable_filename" \ + " exec_changed_state\[2\] = event.reload" \ + "end" ] \ + "build executable_changed function" 0 + + gdb_test_no_output -nopass "python reset_state()" + gdb_test_no_output "python gdb.events.executable_changed.connect(executable_changed)" +} + +# Check the global Python state that is updated when the +# executable_changed event occurs, and then reset the global state. +# FILENAME is a string, the name of the new executable file. RELOAD +# is a string, which should be 'True' or 'False', and represents if +# the executable file was reloaded, or changed. +proc check_exec_change { filename_re reload testname } { + if { $filename_re ne "None" } { + set filename_re "'$filename_re'" + } + if { $filename_re eq "None" && $reload eq "None" } { + set count 0 + } else { + set count 1 + } + gdb_test "python print(exec_changed_state)" \ + "\\\[$count, $filename_re, $reload\\\]" \ + $testname + gdb_test_no_output -nopass "python reset_state()" +} + # Check that the executable_filename is set correctly after using the # 'file' command. with_test_prefix "using 'file' command" { clean_restart + setup_exec_change_handler + gdb_test "python print(gdb.current_progspace().executable_filename)" \ "None" \ "check executable_filename when no file is loaded" @@ -51,6 +99,9 @@ with_test_prefix "using 'file' command" { "[string_to_regexp $binfile1]" \ "check executable_filename when first executable is loaded" + check_exec_change [string_to_regexp $binfile1] False \ + "check executable_changed state after first executable was loaded" + gdb_test "file $binfile2" \ "Reading symbols from [string_to_regexp $binfile2]\\.\\.\\..*" \ "load second executable" \ @@ -59,42 +110,91 @@ with_test_prefix "using 'file' command" { "[string_to_regexp $binfile2]" \ "check executable_filename when second executable is loaded" + check_exec_change [string_to_regexp $binfile2] False \ + "check executable_changed state after second executable was loaded" + gdb_unload gdb_test "python print(gdb.current_progspace().executable_filename)" \ "None" \ "check executable_filename after unloading file" + + check_exec_change None False \ + "check executable_changed state after unloading the executable" } # Check that the executable_filename is correctly set when we only set # the exec-file. with_test_prefix "using 'exec-file' command" { clean_restart + + setup_exec_change_handler + 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" + check_exec_change [string_to_regexp $binfile1] False \ + "check executable_changed state after first executable was 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" + check_exec_change [string_to_regexp $binfile2] False \ + "check executable_changed state after second executable was 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_exec_change None False \ + "check executable_changed state after unloading the executable" } # Check that setting the symbol-file doesn't cause the # executable_filename to be set. with_test_prefix "using 'symbol-file' command" { clean_restart + + setup_exec_change_handler + 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" + + check_exec_change None None \ + "check executable_changed state after setting symbol-file" +} + +# Check the executable_changed event when the executable changes on disk. +with_test_prefix "exec changes on disk" { + clean_restart $binfile1 + + setup_exec_change_handler + + runto_main + + gdb_test_no_output "shell sleep 1" \ + "ensure executable is at least 1 second old" + + gdb_test "shell touch ${binfile1}" "" \ + "update the executable on disk" + + runto_main + + # There is currently an issue where the executable_changed event + # will trigger twice during an inferior restart. This should be + # fixed in the next commit, at which point this kfail can be + # removed. + setup_kfail "????" *-*-* + check_exec_change [string_to_regexp $binfile1] True \ + "check executable_changed state after exec changed on disk" } -- 2.25.4