binutils-gdb/gdb/testsuite/gdb.python/py-selected-context.c
Andrew Burgess ee89a3c9ef gdb/python: new selected_context event
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 <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2026-03-05 09:42:18 +00:00

56 lines
1.3 KiB
C

/* 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 <http://www.gnu.org/licenses/>. */
#include <pthread.h>
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. */
}