From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id bLawHDKWqGlYfxQAWB0awg (envelope-from ) for ; Wed, 04 Mar 2026 15:29:38 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OILjW6vJ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 528131E0DD; Wed, 04 Mar 2026 15:29:38 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 949B41E089 for ; Wed, 04 Mar 2026 15:29:36 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 0DF5D4BA23D5 for ; Wed, 4 Mar 2026 20:29:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0DF5D4BA23D5 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OILjW6vJ Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id AC61E4BA2E08 for ; Wed, 4 Mar 2026 20:29:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC61E4BA2E08 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AC61E4BA2E08 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1772656145; cv=none; b=g35WZynpZL6x01BnSUr4gXw7ycetd1ZYLVzbTc6vRGSsxDCiltO5ZnOZPwTBwMEQu+1lhFZVIeDNb6lxt3ADlkjS9okGm5oJN++49hCP7/GiNGArJtFbpy5y0JHhTdLnwMcIy+5BAQOXQQpIgvkAfMHg99CVxNU94c46twSExHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1772656145; c=relaxed/simple; bh=y5uTWfAv5zTzZIhbtBmWbcopkusSqO2LL2BYAmPOGAc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=eCbR6ar4BFx2VN/G7JJ8kYPw8mYaq1jqXheXUhxyujp1CApSiQFebj3/tprW9OoKnhbwt7mD5/D2fADoM1hx/neJ+5iAktBOQE2rhEo3pwfpwaZyZ26COJecba4RxYU4iwd+nvjH07lwqa6DkgGDVZqyHeH7kCE+aDiVaobagH8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC61E4BA2E08 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772656145; 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=LGKG2NVaWtIkUjJDepTqxclWutehpkz4q0XUnp3JrEE=; b=OILjW6vJlSqMdHaqpgtw88RGqQZoD41xQdFJ8CGfYCSp4+ofmVdyu+pJ7NS7AEoV91l8N8 Zz98qhtLrFHn1VRtDDArEpPlK5k4HzJc0T/zsSW6O4JdnEtwBrkOzflX7I/0MDsuXaHIfz DMKyDc6fsTyJY1ivJxlCawIWAhlOCQA= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-42-ntVKrPB2PjyDOFaq5KzvCg-1; Wed, 04 Mar 2026 15:29:04 -0500 X-MC-Unique: ntVKrPB2PjyDOFaq5KzvCg-1 X-Mimecast-MFC-AGG-ID: ntVKrPB2PjyDOFaq5KzvCg_1772656143 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-439a85832c0so3378906f8f.2 for ; Wed, 04 Mar 2026 12:29:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772656142; x=1773260942; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=LGKG2NVaWtIkUjJDepTqxclWutehpkz4q0XUnp3JrEE=; b=ZCnwzUAI8MuTtseJxmu3H3Mnwm5tTo//ZvctI/SSx6ecFxa8zMKevEj551ShUE4wTM pgORu7Imk+/A/jxD/6sj7dnqp9zBzIlwdZ7f1LO+raINzI3T/6k0LCUVlBLvXP6bxLWS iXeAM4o9fs2h/PxFAndCSartKE9lqd4rRYC3Y5lKR5mI4Zdz3NrTlpYGeX+nkD4i+VsH x/9fYQKKIpwlTWXIYiyG6fr1UTAf7Zu7/8gY8mLZ1citvkN4Iv0TWh7coxDfDIBy4Fyi 9V4D0uSzaWXMXiNbtVXkx9IJ1UoLUEKYnQqOwMEQUfmgPs9R50RA6h1ggkkEsw0qKJcT uQoA== X-Gm-Message-State: AOJu0YyJoolaGncKguvo0tGEt1gF9L/J309axROIgC7EEwg9rZZ2vwC8 MWBkFDSiRiDXeiRrkJkoeJn2l7JJ9wl4Ho3w/Oy/VN9Tng6qGFki9DtvvJVU4Iswbx7Rb5KHg0H VIUpqR0piJoXR5NZkdhvFP60XvR4BT7yUBsOMKxe52954rDOQtTCgNJTLaFRrYuGU2KFUPKLwNK OEVR/gqMdI2XEvaxCvTTSIgNnhb6K5rleYI3/GpbQdGuyg+to= X-Gm-Gg: ATEYQzx18bxybIGBObqo62fmqaWs4w4VWbWuqlzig4IYEzIsEO++90bluXOg2LKERZE HxHK8qbvcMCiGHNtWpBK1FX31TK8wP9gwm6jjMy1VqXaiBztbfJX6UI7BOUq1J6N4uVOeXkw7Bz X2SCkSUalLYQtK3acwIQjw7iDl4bUu6hx6iAKvzDN5VPZD5AmRFdlrSD5/BUieb30pqEjXip5+4 v3KvVVEla8HiQyZ99qBrbOm6Km7VWllB1FMmU+xoK7CAnsC3JBOSv3tPTylqogY0zAfZCXKiXsC CNMnptxBfEwxJlIyZd4r6DbcdKJFpNanaK56t/EhxlfaxDnC1KAGHSOy1m/7//SjXnSESR+n4fn lT9y7i7jD9wuoGrITCzb3RZ5N/cFR9hm/DPgK8cPRgHY= X-Received: by 2002:a05:6000:2482:b0:439:b522:37f6 with SMTP id ffacd0b85a97d-439c7fd3d06mr6407888f8f.35.1772656142151; Wed, 04 Mar 2026 12:29:02 -0800 (PST) X-Received: by 2002:a05:6000:2482:b0:439:b522:37f6 with SMTP id ffacd0b85a97d-439c7fd3d06mr6407836f8f.35.1772656141542; Wed, 04 Mar 2026 12:29:01 -0800 (PST) Received: from localhost (92.40.184.81.threembb.co.uk. [92.40.184.81]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439ab6ebe56sm34706809f8f.15.2026.03.04.12.29.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2026 12:29:01 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv2] gdb/python: new selected_context event Date: Wed, 4 Mar 2026 20:28:56 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: <90f187ce8c819e25bdc49101f4579490cf267ded.1771776161.git.aburgess@redhat.com> References: <90f187ce8c819e25bdc49101f4579490cf267ded.1771776161.git.aburgess@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Npz8x00alKRU1Vhbh64oO3rGQ4hVrS1nX1sVpRXyRBk_1772656143 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org In v2: - Renamed event to SelectedContextEvent, adding the 'Event' suffix. - Fixed the reference counting bug for Py_None, now use gdbpy_ref<>::new_reference (Py_None). - Rebased to upstream HEAD. --- This commit introduces a new Python event, selected_context. This event is attached to the user_selected_context_changed observer, which triggers when the user changes the currently selected inferior, thread, or frame. Adding this event allows a Python extension to update in response to user driven changes without having to poll the state from a before_prompt hook, which is what I currently do to achieve the same results. I did consider splitting the user_selected_context_changed observer into 3 separate Python events, inferior_changed, thread_changed, and frame_changed, but I couldn't see any significant advantage to doing this, so in the end I went with just a single event, and the event object contains the inferior, thread, and frame. Additionally, the user isn't informed about which aspect of the context changed. That is, every event carries the inferior, thread, and frame, so an event triggered when switching frames will looks identical to an event triggered when switching inferiors. If the user wants to know what changed then they will have to track the current state themselves, and then compare the event state to the stored current state. In many cases though I suspect that just being told something changed, and then updating everything will be sufficient, which is why I've not bothered trying to inform the user what changed. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=24482 Reviewed-By: Eli Zaretskii --- gdb/NEWS | 7 + gdb/doc/python.texi | 35 +++++ gdb/python/py-all-events.def | 1 + gdb/python/py-event-types.def | 5 + gdb/python/py-inferior.c | 54 ++++++++ .../gdb.python/py-selected-context.c | 56 ++++++++ .../gdb.python/py-selected-context.exp | 130 ++++++++++++++++++ .../gdb.python/py-selected-context.py | 57 ++++++++ 8 files changed, 345 insertions(+) create mode 100644 gdb/testsuite/gdb.python/py-selected-context.c create mode 100644 gdb/testsuite/gdb.python/py-selected-context.exp create mode 100644 gdb/testsuite/gdb.python/py-selected-context.py diff --git a/gdb/NEWS b/gdb/NEWS index d70147144e4..72471dbc751 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -200,6 +200,13 @@ qExecAndArgs contains the gdb.Corefile object if a core file is loaded into the inferior, otherwise, this contains None. + ** New event registry gdb.events.selected_context that emits a + SelectedContextEvent event whenever the user changes the inferior + context. The context consists of which inferior, thread, and + frame are currently selected. The event object has 'inferior', + 'thread' and 'frame' attributes containing gdb.Inferior, + gdb.InferiorThread, and gdb.Frame objects respectively. + * Guile API ** Procedures 'memory-port-read-buffer-size', diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 1f88ea7e9ad..6f1f35101f9 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4059,6 +4059,41 @@ Events In Python The exiting thread. @end defvar +@item events.selected_context +This is emitted when the user directly, or indirectly, causes the +selected inferior context to change. The context consists of the +currently selected inferior, thread, and frame. Examples commands +that trigger this event are @kbd{inferior}, @kbd{thread}, and +@kbd{frame}. + +The event is of type @code{gdb.SelectedContextEvent} and has the +following attributes: + +@defvar SelectedContextEvent.inferior +The currently selected inferior. This is of type @code{gdb.Inferior} +(@pxref{Inferiors In Python}). +@end defvar + +@defvar SelectedContextEvent.thread +The currently selected thread. If not @code{None} then this is of +type @code{gdb.InferiorThread} (@pxref{Threads In Python}). If +switching to an inferior that is not yet started, then the +@code{thread} attribute will be @code{None}. +@end defvar + +@defvar SelectedContextEvent.frame +The currently selected frame. If not @code{None} then this is of type +@code{gdb.Frame} (@pxref{Frames In Python}). If switching to an +inferior that is not yet started, then the @code{frame} attribute will +be @code{None}. +@end defvar + +In some cases @value{GDBN} might emit the @code{selected_context} +event even when the context has not changed. The state within the +event will always reflect the state of the current inferior. These +unnecessary events could be removed in future releases of +@value{GDBN}. + @item events.gdb_exiting This is emitted when @value{GDBN} exits. This event is not emitted if @value{GDBN} exits as a result of an internal error, or after an diff --git a/gdb/python/py-all-events.def b/gdb/python/py-all-events.def index b88d11ad4f2..24724038562 100644 --- a/gdb/python/py-all-events.def +++ b/gdb/python/py-all-events.def @@ -46,3 +46,4 @@ GDB_PY_DEFINE_EVENT(executable_changed) GDB_PY_DEFINE_EVENT(new_progspace) GDB_PY_DEFINE_EVENT(free_progspace) GDB_PY_DEFINE_EVENT(tui_enabled) +GDB_PY_DEFINE_EVENT(selected_context) diff --git a/gdb/python/py-event-types.def b/gdb/python/py-event-types.def index a7644d79b06..fe3e0978a55 100644 --- a/gdb/python/py-event-types.def +++ b/gdb/python/py-event-types.def @@ -145,3 +145,8 @@ GDB_PY_DEFINE_EVENT_TYPE (tui_enabled, "TuiEnabledEvent", "GDB TUI enabled event object", event_object_type); + +GDB_PY_DEFINE_EVENT_TYPE (selected_context, + "SelectedContextEvent", + "GDB user selected context event object", + event_object_type); diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index ae309620e1f..76e3da9f620 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -997,6 +997,58 @@ gdbpy_selected_inferior (PyObject *self, PyObject *args) inferior_to_inferior_object (current_inferior ()).release ()); } +/* Implement the selected_context event handler. This is called when some + aspect of the inferior's context (inferior, thread, or frame) is + changed by the user. If there are event listeners in place then create + an event object and notify the listeners. */ + +static void +python_context_changed (user_selected_what selection) +{ + if (!gdb_python_initialized) + return; + + gdbpy_enter enter_py (current_inferior ()->arch ()); + + if (evregpy_no_listeners_p (gdb_py_events.selected_context)) + return; + + gdbpy_ref<> inf_obj (gdbpy_selected_inferior (nullptr, nullptr)); + if (inf_obj == nullptr) + { + gdbpy_print_stack (); + return; + } + + gdbpy_ref<> thr_obj (gdbpy_selected_thread (nullptr, nullptr)); + if (thr_obj == nullptr) + { + gdbpy_print_stack (); + return; + } + + gdbpy_ref<> frame_obj; + if (has_stack_frames ()) + frame_obj = gdbpy_ref<> (gdbpy_selected_frame (nullptr, nullptr)); + else + frame_obj = gdbpy_ref<>::new_reference (Py_None); + + if (frame_obj == nullptr) + { + gdbpy_print_stack (); + return; + } + + gdbpy_ref<> event + = create_event_object (&selected_context_event_object_type); + if (event == nullptr + || evpy_add_attribute (event.get (), "inferior", inf_obj.get ()) < 0 + || evpy_add_attribute (event.get (), "thread", thr_obj.get ()) < 0 + || evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0 + || evpy_emit_event (event.get (), gdb_py_events.selected_context) < 0) + gdbpy_print_stack (); +} + static int gdbpy_initialize_inferior () { @@ -1027,6 +1079,8 @@ gdbpy_initialize_inferior () gdb::observers::inferior_added.attach (python_new_inferior, "py-inferior"); gdb::observers::inferior_removed.attach (python_inferior_deleted, "py-inferior"); + gdb::observers::user_selected_context_changed.attach (python_context_changed, + "py-inferior"); return 0; } diff --git a/gdb/testsuite/gdb.python/py-selected-context.c b/gdb/testsuite/gdb.python/py-selected-context.c new file mode 100644 index 00000000000..b7654829a24 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-selected-context.c @@ -0,0 +1,56 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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 . */ + +#include + +volatile int global_var = 0; + +/* Thread inner function. */ + +void +thread_breakpt (void) +{ + global_var = global_var + 1; /* First breakpoint. */ +} + +/* The thread entry point. */ + +void * +worker_thread (void *unused) +{ + thread_breakpt (); + return NULL; +} + +/* Create a thread, and wait for it to complete. */ + +void +run_thread (void) +{ + pthread_t thr; + + pthread_create (&thr, NULL, worker_thread, NULL); + + pthread_join (thr, NULL); +} + +int +main (void) +{ + run_thread (); + return 0; /* Second breakpoint. */ +} diff --git a/gdb/testsuite/gdb.python/py-selected-context.exp b/gdb/testsuite/gdb.python/py-selected-context.exp new file mode 100644 index 00000000000..c287cc90d44 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-selected-context.exp @@ -0,0 +1,130 @@ +# Copyright (C) 2026 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 . + +# Check the Python gdb.selected_context event handling. + +require allow_python_tests + +load_lib gdb-python.exp + +standard_testfile + +if { [build_executable "build exec" $testfile $srcfile {debug pthreads}] } { + return +} + +clean_restart + +# Source the Python script. +set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py] +gdb_test "source ${pyfile}" "^DONE" "load python file" +gdb_test "test-selected-context-event" \ + "^GDB selected-context event registered\\." + +# Return a regexp for when the selected context event triggers, and +# runs without error. +proc event_regexp { inferior {thread "None"} {frame "None"}} { + return [multi_line \ + " Inferior: ${inferior}" \ + " Thread: [string_to_regexp $thread]" \ + " Frame: [string_to_regexp $frame]"] +} + +# Use 'info inferiors' to check that INF is the currently selected +# inferior. INF should be an inferior number, e.g. '1', '2', etc. +proc check_inferior { inf testname } { + gdb_test "info inferiors" \ + "\r\n\\*\\s+[string_to_regexp $inf]\\s+\[^\r\n\]*(?=\r\n)" \ + $testname +} + +# Use 'info threads' to check that THR is the currently selected +# thread. THR should be the thread-id (e.g. '1.1', '2.1') as appears +# in the 'info threads' output. +proc check_thread { thr testname } { + gdb_test "info threads" \ + "\r\n\\*\\s+[string_to_regexp $thr]\\s+\[^\r\n\]+(?=\r\n).*" \ + $testname +} + +# Create a second inferior. +gdb_test "add-inferior" "Added inferior 2\[^\r\n\]*" + +# Switch between inferiors before either inferior is started. The +# event will include a valid gdb.Inferior, but the thread and frame +# will both be None. +gdb_test "inferior 2" [event_regexp 2] \ + "switch to inferior 2, inferior is not started" +gdb_test "inferior 1" [event_regexp 1] \ + "switch to inferior 1, inferior is not started" + +# Arrange for the event handler to raise an error. Switch inferior, +# check the error is printed, then check that the inferior switch was +# still successful. +gdb_test_no_output "python event_throws_error = True" +gdb_test "inferior 2" \ + [multi_line \ + "\\\[Switching to inferior 2\[^\r\n\]*\\\]" \ + "\[^\r\n\]+: error from gdb_selected_context_handler"] \ + "switch to inferior 2, event raises an error" +check_inferior 2 "check inferior 2 was selected" + +# Switch back to inferior 1. +gdb_test "inferior 1" ".*" \ + "return to inferior 1" + +# Load the executable and start the inferior. +gdb_load $binfile +if {![runto_main]} { + return +} + +# Setup breakpoints and continue until the first is reached. +gdb_breakpoint [gdb_get_line_number "First breakpoint"] +gdb_breakpoint [gdb_get_line_number "Second breakpoint"] +gdb_continue_to_breakpoint "first bp" + +# Ensure the expected thread is currently selected. +check_thread 1.2 "confirm expected thread selected" + +# Switch thread. The event handler is still configured to raise an +# error, but the thread switch should still happen. +gdb_test "thread 1" \ + [multi_line \ + "\\\[Switching to thread 1\\.1\[^\r\n\]*\\\]" \ + "#0\\s+\[^\r\n\]+" \ + "\[^\r\n\]+: error from gdb_selected_context_handler"] \ + "switch thread, handler raises an error" +check_thread 1.1 "thread switched despite handler error" + +# Switch frame, ensure event handler raises an error. +gdb_test "up" \ + "#1\\s+.*: error from gdb_selected_context_handler" \ + "error from event handler when switching frames" + +# Disable handler errors. +gdb_test_no_output "python event_throws_error = False" + +# Switch thread, ensure event handler triggers. +gdb_test "thread 2" [event_regexp 1 1.2 #0] \ + "switch to thread 2, event handler triggers" + +# Now switch frames, ensure the event handler triggers. +gdb_test "up" [event_regexp 1 1.2 #1] \ + "move up a frame, event handler triggers" +gdb_test "down" [event_regexp 1 1.2 #0] \ + "move down a frame, event handler triggers" +gdb_test "frame 1" [event_regexp 1 1.2 #1] \ + "select a frame, event handler triggers" diff --git a/gdb/testsuite/gdb.python/py-selected-context.py b/gdb/testsuite/gdb.python/py-selected-context.py new file mode 100644 index 00000000000..e624be71994 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-selected-context.py @@ -0,0 +1,57 @@ +# Copyright (C) 2026 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 . + +import gdb + +event_throws_error = False + +def gdb_selected_context_handler(event): + assert isinstance(event, gdb.SelectedContextEvent) + + global event_throws_error + + if event_throws_error: + raise gdb.GdbError("error from gdb_selected_context_handler") + else: + print("event type: selected-context") + assert isinstance(event.inferior, gdb.Inferior) + print(" Inferior: %d" % (event.inferior.num)) + if event.thread is None: + thr = "None" + else: + assert isinstance(event.thread, gdb.InferiorThread) + thr = "%d.%d" % (event.thread.inferior.num, event.thread.num) + print(" Thread: %s" % (thr)) + if event.frame is None: + frame = "None" + else: + assert isinstance(event.frame, gdb.Frame) + frame = "#%d" % (event.frame.level()) + print(" Frame: %s" % (frame)) + + +class test_selected_context(gdb.Command): + """Test GDB's Selected Context Event.""" + + def __init__(self): + gdb.Command.__init__(self, "test-selected-context-event", gdb.COMMAND_USER) + + def invoke(self, arg, from_tty): + gdb.events.selected_context.connect(gdb_selected_context_handler) + print("GDB selected-context event registered.") + +test_selected_context() + +print("DONE") base-commit: c2af35131a7c9fd9f6b3192418baf366b125efa4 -- 2.25.4