Skip to content

Commit 0ae1a32

Browse files
author
Simon Marchi
committed
python: Add Progspace.objfiles method
This patch adds an objfiles method to the Progspace object, which returns a sequence of the objfiles associated to that program space. I chose a method rather than a property for symmetry with gdb.objfiles(). gdb/ChangeLog: * python/py-progspace.c (PSPY_REQUIRE_VALID): New macro. (pspy_get_objfiles): New function. (progspace_object_methods): New. (pspace_object_type): Add tp_methods callback. * python/python-internal.h (build_objfiles_list): New declaration. * python/python.c (build_objfiles_list): New function. (gdbpy_objfiles): Implement using build_objfiles_list. * NEWS: Mention the Progspace.objfiles method. gdb/doc/ChangeLog: * python.texi (Program Spaces In Python): Document the Progspace.objfiles method. (Objfiles In Python): Mention that gdb.objfiles() is identical to gdb.selected_inferior().progspace.objfiles(). gdb/testsuite/ChangeLog: * gdb.python/py-progspace.exp: Test the Progspace.objfiles method.
1 parent a40bf0c commit 0ae1a32

9 files changed

Lines changed: 122 additions & 14 deletions

File tree

‎gdb/ChangeLog‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2018-09-13 Simon Marchi <simon.marchi@ericsson.com>
2+
2018-09-13 Tom Tromey <tom@tromey.com>
3+
4+
* python/py-progspace.c (PSPY_REQUIRE_VALID): New macro.
5+
(pspy_get_objfiles): New function.
6+
(progspace_object_methods): New.
7+
(pspace_object_type): Add tp_methods callback.
8+
* python/python-internal.h (build_objfiles_list): New
9+
declaration.
10+
* python/python.c (build_objfiles_list): New function.
11+
(gdbpy_objfiles): Implement using build_objfiles_list.
12+
* NEWS: Mention the Progspace.objfiles method.
13+
114
2018-09-13 Simon Marchi <simon.marchi@ericsson.com>
215

316
* python/py-inferior.c (infpy_get_progspace): New function.

‎gdb/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ CSKY GNU/LINUX csky*-*-linux
8989
** The gdb.Inferior type has a new 'progspace' property, which is the program
9090
space associated to that inferior.
9191

92+
** The gdb.Progspace type has a new 'objfiles' method, which returns the list
93+
of objfiles associated to that program space.
94+
9295
*** Changes in GDB 8.2
9396

9497
* The 'set disassembler-options' command now supports specifying options

‎gdb/doc/ChangeLog‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2018-09-13 Simon Marchi <simon.marchi@ericsson.com>
2+
2018-09-13 Tom Tromey <tom@tromey.com>
3+
4+
* python.texi (Program Spaces In Python): Document the
5+
Progspace.objfiles method.
6+
(Objfiles In Python): Mention that gdb.objfiles() is identical
7+
to gdb.selected_inferior().progspace.objfiles().
8+
19
2018-09-13 Simon Marchi <simon.marchi@ericsson.com>
210
2018-09-13 Tom Tromey <tom@tromey.com>
311

‎gdb/doc/python.texi‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4079,6 +4079,14 @@ Hello.
40794079
[Inferior 1 (process 4242) exited normally]
40804080
@end smallexample
40814081

4082+
A @code{gdb.Progspace} object has the following methods:
4083+
4084+
@findex Progspace.objfiles
4085+
@defun Progspace.objfiles ()
4086+
Return a sequence of all the objfiles referenced by this program
4087+
space. @xref{Objfiles In Python}.
4088+
@end defun
4089+
40824090
@node Objfiles In Python
40834091
@subsubsection Objfiles In Python
40844092

@@ -4105,7 +4113,9 @@ this function returns @code{None}.
41054113
@findex gdb.objfiles
41064114
@defun gdb.objfiles ()
41074115
Return a sequence of all the objfiles current known to @value{GDBN}.
4108-
@xref{Objfiles In Python}.
4116+
@xref{Objfiles In Python}. This is identical to
4117+
@code{gdb.selected_inferior().progspace.objfiles()} (@pxref{Progspaces In
4118+
Python}) and is included for historical compatibility.
41094119
@end defun
41104120

41114121
@findex gdb.lookup_objfile

‎gdb/python/py-progspace.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ extern PyTypeObject pspace_object_type
5858

5959
static const struct program_space_data *pspy_pspace_data_key;
6060

61-
61+
/* Require that PSPACE_OBJ be a valid program space ID. */
62+
#define PSPY_REQUIRE_VALID(pspace_obj) \
63+
do { \
64+
if (pspace_obj->pspace == nullptr) \
65+
{ \
66+
PyErr_SetString (PyExc_RuntimeError, \
67+
_("Program space no longer exists.")); \
68+
return NULL; \
69+
} \
70+
} while (0)
6271

6372
/* An Objfile method which returns the objfile's file name, or None. */
6473

@@ -314,7 +323,17 @@ pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
314323
return 0;
315324
}
316325

317-
326+
/* Implement the objfiles method. */
327+
328+
static PyObject *
329+
pspy_get_objfiles (PyObject *self_, PyObject *args)
330+
{
331+
pspace_object *self = (pspace_object *) self_;
332+
333+
PSPY_REQUIRE_VALID (self);
334+
335+
return build_objfiles_list (self->pspace).release ();
336+
}
318337

319338
/* Clear the PSPACE pointer in a Pspace object and remove the reference. */
320339

@@ -397,6 +416,13 @@ static gdb_PyGetSetDef pspace_getset[] =
397416
{ NULL }
398417
};
399418

419+
static PyMethodDef progspace_object_methods[] =
420+
{
421+
{ "objfiles", pspy_get_objfiles, METH_NOARGS,
422+
"Return a sequence of objfiles associated to this program space." },
423+
{ NULL }
424+
};
425+
400426
PyTypeObject pspace_object_type =
401427
{
402428
PyVarObject_HEAD_INIT (NULL, 0)
@@ -426,7 +452,7 @@ PyTypeObject pspace_object_type =
426452
0, /* tp_weaklistoffset */
427453
0, /* tp_iter */
428454
0, /* tp_iternext */
429-
0, /* tp_methods */
455+
progspace_object_methods, /* tp_methods */
430456
0, /* tp_members */
431457
pspace_getset, /* tp_getset */
432458
0, /* tp_base */

‎gdb/python/python-internal.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
549549
struct frame_info *frame_object_to_frame_info (PyObject *frame_obj);
550550
struct gdbarch *arch_object_to_gdbarch (PyObject *obj);
551551

552+
/* Return a Python list containing an Objfile object for each objfile in
553+
PSPACE. */
554+
gdbpy_ref<> build_objfiles_list (program_space *pspace);
555+
552556
void gdbpy_initialize_gdb_readline (void);
553557
int gdbpy_initialize_auto_load (void)
554558
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;

‎gdb/python/python.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,26 +1437,34 @@ gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
14371437
return result;
14381438
}
14391439

1440-
/* Return a sequence holding all the Objfiles. */
1440+
/* See python-internal.h. */
14411441

1442-
static PyObject *
1443-
gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
1442+
gdbpy_ref<>
1443+
build_objfiles_list (program_space *pspace)
14441444
{
14451445
struct objfile *objf;
14461446

14471447
gdbpy_ref<> list (PyList_New (0));
14481448
if (list == NULL)
14491449
return NULL;
14501450

1451-
ALL_OBJFILES (objf)
1452-
{
1453-
PyObject *item = objfile_to_objfile_object (objf);
1451+
ALL_PSPACE_OBJFILES (pspace, objf)
1452+
{
1453+
PyObject *item = objfile_to_objfile_object (objf);
14541454

1455-
if (!item || PyList_Append (list.get (), item) == -1)
1456-
return NULL;
1457-
}
1455+
if (item == nullptr || PyList_Append (list.get (), item) == -1)
1456+
return NULL;
1457+
}
14581458

1459-
return list.release ();
1459+
return list;
1460+
}
1461+
1462+
/* Return a sequence holding all the Objfiles. */
1463+
1464+
static PyObject *
1465+
gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
1466+
{
1467+
return build_objfiles_list (current_program_space).release ();
14601468
}
14611469

14621470
/* Compute the list of active python type printers and store them in

‎gdb/testsuite/ChangeLog‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2018-09-13 Simon Marchi <simon.marchi@ericsson.com>
2+
3+
* gdb.python/py-progspace.exp: Test the Progspace.objfiles
4+
method.
5+
16
2018-09-13 Simon Marchi <simon.marchi@ericsson.com>
27

38
* gdb.python/py-inferior.exp: Add tests for Inferior.progspace

‎gdb/testsuite/gdb.python/py-progspace.exp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,34 @@ gdb_py_test_silent_cmd "python progspace.random_attribute = 42" \
5151
"Set random attribute in progspace" 1
5252
gdb_test "python print (progspace.random_attribute)" "42" \
5353
"Verify set of random attribute in progspace"
54+
55+
if {![runto_main]} {
56+
fail "can't run to main"
57+
return
58+
}
59+
60+
# With a single inferior, progspace.objfiles () and gdb.objfiles () should
61+
# be identical.
62+
gdb_test "python print (progspace.objfiles () == gdb.objfiles ())" "True"
63+
64+
gdb_test "add-inferior"
65+
gdb_test "inferior 2"
66+
67+
gdb_load ${binfile}
68+
69+
# With a second (non-started) inferior, we should have a single objfile - the
70+
# main one.
71+
gdb_test "python print (len (gdb.objfiles ())) == 1"
72+
73+
# And the gdb.objfiles() list should now be different from the objfiles of the
74+
# prog space of inferior 1.
75+
gdb_test "python print (progspace.objfiles () != gdb.objfiles ())" "True"
76+
77+
# Delete inferior 2 (and therefore the second progspace), check that the Python
78+
# object reacts sensibly.
79+
gdb_py_test_silent_cmd "python progspace2 = gdb.current_progspace()" \
80+
"save progspace 2" 1
81+
gdb_test "inferior 1" "Switching to inferior 1.*"
82+
gdb_test_no_output "remove-inferiors 2"
83+
gdb_test "python print (progspace2.objfiles ())" \
84+
"RuntimeError: Program space no longer exists.*"

0 commit comments

Comments
 (0)