Skip to content

Commit 59912fb

Browse files
committed
gdb: add Python events for program space addition and removal
Initially I just wanted a Python event for when GDB removes a program space, I'm writing a Python extension that caches information for each program space, and need to know when I should discard entries for a particular program space. But, it seemed easy enough to also add an event for when GDB adds a new program space, so I went ahead and added both new events. Of course, we don't currently have an observable for program space addition or removal, so I first needed to add these. After that it's pretty simple to add two new Python events and have these trigger. The two new event registries are: events.new_progspace events.free_progspace These emit NewProgspaceEvent and FreeProgspaceEvent objects respectively, each of these new event types has a 'progspace' attribute that contains the relevant gdb.Progspace object. There's a couple of things to be mindful of. First, it is not possible to catch the NewProgspaceEvent for the very first program space, the one that is created when GDB first starts, as this program space is created before any Python scripts are sourced. In order to allow this event to be caught we would need to defer creating the first program space, and as a consequence the first inferior, until some later time. But, existing scripts could easily depend on there being an initial inferior, so I really don't think we should change that -- and so, we end up with the consequence that we can't catch the event for the first program space. The second, I think minor, issue, is that GDB doesn't clean up its program spaces upon exit -- or at least, they are not cleaned up before Python is shut down. As a result, any program spaces in use at the time GDB exits don't generate a FreeProgspaceEvent. I'm not particularly worried about this for my use case, I'm using the event to ensure that a cache doesn't hold stale entries within a single GDB session. It's also easy enough to add a Python at-exit callback which can do any final cleanup if needed. Finally, when testing, I did hit a slightly weird issue with some of the remote boards (e.g. remote-stdio-gdbserver). As a consequence of this issue I see some output like this in the gdb.log: (gdb) PASS: gdb.python/py-progspace-events.exp: inferior 1 step FreeProgspaceEvent: <gdb.Progspace object at 0x7fb7e1d19c10> warning: cannot close "target:/lib64/libm.so.6": Cannot execute this command while the target is running. Use the "interrupt" command to stop the target and then try again. warning: cannot close "target:/lib64/libc.so.6": Cannot execute this command while the target is running. Use the "interrupt" command to stop the target and then try again. warning: cannot close "target:/lib64/ld-linux-x86-64.so.2": Cannot execute this command while the target is running. Use the "interrupt" command to stop the target and then try again. do_parent_stuff () at py-progspace-events.c:41 41 ++global_var; (gdb) PASS: gdb.python/py-progspace-events.exp: step The 'FreeProgspaceEvent ...' line is expected, that's my test Python extension logging the event. What isn't expected are all the blocks like: warning: cannot close "target:/lib64/libm.so.6": Cannot execute this command while the target is running. Use the "interrupt" command to stop the target and then try again. It turns out that this has nothing to do with my changes, this is just a consequence of reading files over the remote protocol. The test forks a child process which GDB stays attached too. When the child exits, GDB cleans up by calling prune_inferiors, which in turn can result in GDB trying to close some files that are open because of the inferior being deleted. If the prune_inferiors call occurs when the remote target is running (and in non-async mode) then GDB will try to send a fileio packet while the remote target is waiting for a stop reply, and the remote target will throw an error, see remote_target::putpkt_binary in remote.c for details. I'm going to look at fixing this, but, as I said, this is nothing to do with this change, I just mention it because I ended up needing to account for these warning messages in one of my tests, and it all looks a bit weird. Approved-By: Tom Tromey <tom@tromey.com> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
1 parent bd93891 commit 59912fb

11 files changed

+349
-0
lines changed

gdb/NEWS

+7
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ show tui mouse-events
319319
gdb.Progspace) and 'reload' (a Boolean) attributes. This event
320320
is emitted when gdb.Progspace.executable_filename changes.
321321

322+
** New event registries gdb.events.new_progspace and
323+
gdb.events.free_progspace, these emit NewProgspaceEvent and
324+
FreeProgspaceEvent event types respectively. Both of these event
325+
types have a single 'progspace' attribute, which is the
326+
gdb.Progspace that is either being added to GDB, or removed from
327+
GDB.
328+
322329
*** Changes in GDB 13
323330

324331
* MI version 1 is deprecated, and will be removed in GDB 14.

gdb/doc/python.texi

+29
Original file line numberDiff line numberDiff line change
@@ -3962,6 +3962,35 @@ command, @value{GDBN} updates both of these fields, but the executable
39623962
file is updated first, so when this event is emitted, the executable
39633963
filename will have changed, but the symbol filename might still hold
39643964
its previous value.
3965+
3966+
@item events.new_progspace
3967+
This is emitted when @value{GDBN} adds a new program space
3968+
(@pxref{Progspaces In Python,,Program Spaces In Python}). The event
3969+
is of type @code{gdb.NewProgspaceEvent}, and has a single read-only
3970+
attribute:
3971+
3972+
@defvar NewProgspaceEvent.progspace
3973+
The @code{gdb.Progspace} that was added to @value{GDBN}.
3974+
@end defvar
3975+
3976+
No @code{NewProgspaceEvent} is emitted for the very first program
3977+
space, which is assigned to the first inferior. This first program
3978+
space is created within @value{GDBN} before any Python scripts are
3979+
sourced.
3980+
3981+
@item events.free_progspace
3982+
This is emitted when @value{GDBN} removes a program space
3983+
(@pxref{Progspaces In Python,,Program Spaces In Python}), for example
3984+
as a result of the @kbd{remove-inferiors} command
3985+
(@pxref{remove_inferiors_cli,,@kbd{remove-inferiors}}). The event is
3986+
of type @code{gdb.FreeProgspaceEvent}, and has a single read-only
3987+
attribute:
3988+
3989+
@defvar FreeProgspaceEvent.progspace
3990+
The @code{gdb.Progspace} that is about to be removed from
3991+
@value{GDBN}.
3992+
@end defvar
3993+
39653994
@end table
39663995

39673996
@node Threads In Python

gdb/observable.c

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ DEFINE_OBSERVABLE (gdb_exiting);
7272
DEFINE_OBSERVABLE (connection_removed);
7373
DEFINE_OBSERVABLE (target_pre_wait);
7474
DEFINE_OBSERVABLE (target_post_wait);
75+
DEFINE_OBSERVABLE (new_program_space);
76+
DEFINE_OBSERVABLE (free_program_space);
7577

7678
} /* namespace observers */
7779
} /* namespace gdb */

gdb/observable.h

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ extern observable <ptid_t /* ptid */> target_pre_wait;
242242
/* About to leave target_wait (). */
243243
extern observable <ptid_t /* event_ptid */> target_post_wait;
244244

245+
/* New program space PSPACE was created. */
246+
extern observable <program_space */* pspace */> new_program_space;
247+
248+
/* The program space PSPACE is about to be deleted. */
249+
extern observable <program_space */* pspace */> free_program_space;
250+
245251
} /* namespace observers */
246252

247253
} /* namespace gdb */

gdb/progspace.c

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "inferior.h"
2929
#include <algorithm>
3030
#include "cli/cli-style.h"
31+
#include "observable.h"
3132

3233
/* The last program space number assigned. */
3334
static int last_program_space_num = 0;
@@ -98,6 +99,7 @@ program_space::program_space (address_space *aspace_)
9899
aspace (aspace_)
99100
{
100101
program_spaces.push_back (this);
102+
gdb::observers::new_program_space.notify (this);
101103
}
102104

103105
/* See progspace.h. */
@@ -106,6 +108,7 @@ program_space::~program_space ()
106108
{
107109
gdb_assert (this != current_program_space);
108110

111+
gdb::observers::free_program_space.notify (this);
109112
remove_program_space (this);
110113

111114
scoped_restore_current_program_space restore_pspace;

gdb/python/py-all-events.def

+2
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ GDB_PY_DEFINE_EVENT(before_prompt)
4343
GDB_PY_DEFINE_EVENT(gdb_exiting)
4444
GDB_PY_DEFINE_EVENT(connection_removed)
4545
GDB_PY_DEFINE_EVENT(executable_changed)
46+
GDB_PY_DEFINE_EVENT(new_progspace)
47+
GDB_PY_DEFINE_EVENT(free_progspace)

gdb/python/py-event-types.def

+10
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,13 @@ GDB_PY_DEFINE_EVENT_TYPE (executable_changed,
130130
"ExecutableChangedEvent",
131131
"GDB executable changed event",
132132
event_object_type);
133+
134+
GDB_PY_DEFINE_EVENT_TYPE (new_progspace,
135+
"NewProgspaceEvent",
136+
"GDB new Progspace event object",
137+
event_object_type);
138+
139+
GDB_PY_DEFINE_EVENT_TYPE (free_progspace,
140+
"FreeProgspaceEvent",
141+
"GDB free Progspace event object",
142+
event_object_type);

gdb/python/py-progspace.c

+63
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,74 @@ gdbpy_executable_changed (struct program_space *pspace, bool reload_p)
643643
gdbpy_print_stack ();
644644
}
645645

646+
/* Helper function to emit NewProgspaceEvent (when ADDING_P is true) or
647+
FreeProgspaceEvent events (when ADDING_P is false). */
648+
649+
static void
650+
gdbpy_program_space_event (program_space *pspace, bool adding_p)
651+
{
652+
if (!gdb_python_initialized)
653+
return;
654+
655+
gdbpy_enter enter_py;
656+
657+
eventregistry_object *registry;
658+
PyTypeObject *event_type;
659+
if (adding_p)
660+
{
661+
registry = gdb_py_events.new_progspace;
662+
event_type = &new_progspace_event_object_type;
663+
}
664+
else
665+
{
666+
registry = gdb_py_events.free_progspace;
667+
event_type = &free_progspace_event_object_type;
668+
}
669+
670+
if (evregpy_no_listeners_p (registry))
671+
return;
672+
673+
gdbpy_ref<> pspace_obj = pspace_to_pspace_object (pspace);
674+
if (pspace_obj == nullptr)
675+
{
676+
gdbpy_print_stack ();
677+
return;
678+
}
679+
680+
gdbpy_ref<> event = create_event_object (event_type);
681+
if (event == nullptr
682+
|| evpy_add_attribute (event.get (), "progspace",
683+
pspace_obj.get ()) < 0
684+
|| evpy_emit_event (event.get (), registry) < 0)
685+
gdbpy_print_stack ();
686+
}
687+
688+
/* Emit a NewProgspaceEvent to indicate PSPACE has been created. */
689+
690+
static void
691+
gdbpy_new_program_space_event (program_space *pspace)
692+
{
693+
gdbpy_program_space_event (pspace, true);
694+
}
695+
696+
/* Emit a FreeProgspaceEvent to indicate PSPACE is just about to be removed
697+
from GDB. */
698+
699+
static void
700+
gdbpy_free_program_space_event (program_space *pspace)
701+
{
702+
gdbpy_program_space_event (pspace, false);
703+
}
704+
646705
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
647706
gdbpy_initialize_pspace (void)
648707
{
649708
gdb::observers::executable_changed.attach (gdbpy_executable_changed,
650709
"py-progspace");
710+
gdb::observers::new_program_space.attach (gdbpy_new_program_space_event,
711+
"py-progspace");
712+
gdb::observers::free_program_space.attach (gdbpy_free_program_space_event,
713+
"py-progspace");
651714

652715
if (PyType_Ready (&pspace_object_type) < 0)
653716
return -1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* This testcase is part of GDB, the GNU debugger.
2+
3+
Copyright 2023 Free Software Foundation, Inc.
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
17+
18+
#include <unistd.h>
19+
#include <sys/types.h>
20+
#include <assert.h>
21+
#include <sys/wait.h>
22+
23+
/* This gives the inferior something to do. */
24+
volatile int global_var = 0;
25+
26+
void
27+
breakpt ()
28+
{ /* Nothing. */ }
29+
30+
void
31+
do_child_stuff ()
32+
{
33+
breakpt ();
34+
++global_var;
35+
}
36+
37+
void
38+
do_parent_stuff ()
39+
{
40+
breakpt ();
41+
++global_var;
42+
}
43+
44+
void
45+
create_child ()
46+
{
47+
int stat;
48+
pid_t wpid;
49+
breakpt ();
50+
pid_t pid = fork ();
51+
assert (pid != -1);
52+
53+
if (pid == 0)
54+
{
55+
/* Child. */
56+
do_child_stuff ();
57+
return;
58+
}
59+
60+
/* Parent. */
61+
do_parent_stuff ();
62+
wpid = waitpid (pid, &stat, 0);
63+
assert (wpid == pid);
64+
}
65+
66+
67+
68+
int
69+
main ()
70+
{
71+
create_child ();
72+
return 0;
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright (C) 2023 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# This file is part of the GDB testsuite. It tests the program space
17+
# related events in the Python API.
18+
19+
load_lib gdb-python.exp
20+
21+
require allow_python_tests
22+
23+
standard_testfile
24+
25+
if {[prepare_for_testing "preparing" $testfile $srcfile] == -1} {
26+
return -1
27+
}
28+
29+
set pyfile [gdb_remote_download host ${srcdir}/${subdir}/py-progspace-events.py]
30+
gdb_test_no_output "source ${pyfile}" "load python file"
31+
32+
if {![runto_main]} {
33+
return
34+
}
35+
36+
gdb_breakpoint breakpt
37+
38+
gdb_continue_to_breakpoint "run to breakpt function"
39+
40+
gdb_test_no_output "set detach-on-fork off"
41+
42+
# Continue until the parent process forks and a new child is added.
43+
# Done this way so we can count the new progspace events; we expect to
44+
# see exactly one.
45+
set new_progspace_event_count 0
46+
gdb_test_multiple "continue" "continue until child process appears" {
47+
-re "^NewProgspaceEvent: <gdb.Progspace object at $hex>\r\n" {
48+
# This is a correctly formed event line.
49+
incr new_progspace_event_count
50+
exp_continue
51+
}
52+
53+
-re "^NewProgspaceEvent:\[^\r\n\]+\r\n" {
54+
# This is an incorrectly formed event line.
55+
fail $gdb_test_name
56+
}
57+
58+
-re "^$gdb_prompt $" {
59+
pass $gdb_test_name
60+
}
61+
62+
-re "^\[^\r\n\]*\r\n" {
63+
exp_continue
64+
}
65+
}
66+
67+
gdb_assert { $new_progspace_event_count == 1 } \
68+
"only a single new progspace event seen"
69+
70+
# Switch to inferior 2 and continue until we hit breakpt.
71+
gdb_test "inferior 2" "\\\[Switching to inferior 2 .*"
72+
gdb_continue_to_breakpoint "run to breakpt in inferior 2"
73+
74+
# Let inferior 2 exit. The new program space is not removed at this
75+
# point.
76+
gdb_test "continue" \
77+
[multi_line \
78+
"^Continuing\\." \
79+
"\\\[Inferior $decimal \[^\r\n\]+ exited normally\\\]"] \
80+
"continue until inferior 2 exits"
81+
82+
gdb_test "inferior 1" "\\\[Switching to inferior 1 .*"
83+
84+
# Step the inferior. During this process GDB will prune the now
85+
# defunct inferior, which deletes its program space, which should
86+
# trigger the FreeProgspaceEvent.
87+
#
88+
# However, there is a slight problem. When the target is remote, and
89+
# GDB is accessing files using remote fileio, then GDB will attempt to
90+
# prune the inferior at a point in time when the remote target is
91+
# waiting for a stop reply. Pruning an inferior causes GDB to close
92+
# files associated with that inferior.
93+
#
94+
# In non-async mode we can't send fileio packets while waiting for a
95+
# stop reply, so the attempts to close files fails, and this shows up
96+
# as an error.
97+
#
98+
# As this error has nothing to do with the feature being tested here,
99+
# we just accept the error message, the important part is the
100+
# 'FreeProgspaceEvent' string, so long as that appears (just once)
101+
# then the test is a success.
102+
set warning_msg \
103+
[multi_line \
104+
"warning: cannot close \"\[^\r\n\]+\": Cannot execute this command while the target is running\\." \
105+
"Use the \"interrupt\" command to stop the target" \
106+
"and then try again\\."]
107+
108+
gdb_test "step" \
109+
[multi_line \
110+
"^FreeProgspaceEvent: <gdb.Progspace object at $hex>(?:\r\n$warning_msg)*" \
111+
"do_parent_stuff \\(\\) at \[^\r\n\]+" \
112+
"$decimal\\s+\[^\r\n\]+"]
113+
114+
# Let this inferior run to completion.
115+
gdb_continue_to_end
116+
117+
# Check the program space events trigger when a new inferior is
118+
# manually added and removed.
119+
gdb_test "add-inferior" \
120+
[multi_line \
121+
"^NewProgspaceEvent: <gdb.Progspace object at $hex>" \
122+
"\\\[New inferior 3\\\]" \
123+
"Added inferior 3\[^\r\n\]*"]
124+
gdb_test "remove-inferior 3" \
125+
"^FreeProgspaceEvent: <gdb.Progspace object at $hex>"

0 commit comments

Comments
 (0)