/* Python interface to inferiors.
- Copyright (C) 2009-2019 Free Software Foundation, Inc.
+ Copyright (C) 2009-2021 Free Software Foundation, Inc.
This file is part of GDB.
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
+#include "auto-load.h"
#include "gdbcore.h"
#include "gdbthread.h"
#include "inferior.h"
static const struct inferior_data *infpy_inf_data_key;
-typedef struct {
+struct membuf_object {
PyObject_HEAD
void *buffer;
/* These are kept just for mbpy_str. */
CORE_ADDR addr;
CORE_ADDR length;
-} membuf_object;
+};
extern PyTypeObject membuf_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("membuf_object");
return;
gdbpy_enter enter_py (objfile != NULL
- ? get_objfile_arch (objfile)
+ ? objfile->arch ()
: target_gdbarch (),
current_language);
return;
gdbpy_ref<> event = create_thread_event_object (&new_thread_event_object_type,
- (PyObject *) thread_obj.get ());
+ (PyObject *) inf_obj);
if (event == NULL
|| evpy_emit_event (event.get (), gdb_py_events.new_thread) < 0)
gdbpy_print_stack ();
INFPY_REQUIRE_VALID (inf);
- return PyLong_FromLong (inf->inferior->num);
+ return gdb_py_object_from_longest (inf->inferior->num).release ();
+}
+
+/* Return the connection number of the given inferior, or None if a
+ connection does not exist. */
+
+static PyObject *
+infpy_get_connection_num (PyObject *self, void *closure)
+{
+ inferior_object *inf = (inferior_object *) self;
+
+ INFPY_REQUIRE_VALID (inf);
+
+ process_stratum_target *target = inf->inferior->process_target ();
+ if (target == nullptr)
+ Py_RETURN_NONE;
+
+ return PyLong_FromLong (target->connection_number);
}
static PyObject *
INFPY_REQUIRE_VALID (inf);
- return PyLong_FromLong (inf->inferior->pid);
+ return gdb_py_object_from_longest (inf->inferior->pid).release ();
}
static PyObject *
return pspace_to_pspace_object (pspace).release ();
}
-static int
-build_inferior_list (struct inferior *inf, void *arg)
-{
- PyObject *list = (PyObject *) arg;
- gdbpy_ref<inferior_object> inferior = inferior_to_inferior_object (inf);
-
- if (inferior == NULL)
- return 0;
-
- return PyList_Append (list, (PyObject *) inferior.get ()) ? 1 : 0;
-}
-
/* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
Returns a tuple of all inferiors. */
PyObject *
if (list == NULL)
return NULL;
- if (iterate_over_inferiors (build_inferior_list, list.get ()))
- return NULL;
+ for (inferior *inf : all_inferiors ())
+ {
+ gdbpy_ref<inferior_object> inferior = inferior_to_inferior_object (inf);
+
+ if (inferior == NULL)
+ continue;
+
+ if (PyList_Append (list.get (), (PyObject *) inferior.get ()) != 0)
+ return NULL;
+ }
return PyList_AsTuple (list.get ());
}
GDB_PY_HANDLE_EXCEPTION (except);
if (found)
- return PyLong_FromLong (found_addr);
+ return gdb_py_object_from_ulongest (found_addr).release ();
else
Py_RETURN_NONE;
}
}
/* Implementation of gdb.Inferior.thread_from_handle (self, handle)
- -> gdb.InferiorThread. */
+ -> gdb.InferiorThread. */
static PyObject *
infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw)
struct thread_info *thread_info;
thread_info = find_thread_by_handle
- (gdb::array_view<const gdb_byte> (bytes, bytes_len),
+ (gdb::array_view<const gdb_byte> (bytes, bytes_len),
inf_obj->inferior);
if (thread_info != NULL)
return thread_to_thread_object (thread_info).release ();
return;
set_inferior_data (inf, infpy_inf_data_key, NULL);
+ Py_TYPE (obj)->tp_free (obj);
}
/* Clear the INFERIOR pointer in an Inferior object and clear the
inferior_to_inferior_object (current_inferior ()).release ());
}
+void _initialize_py_inferior ();
+void
+_initialize_py_inferior ()
+{
+ infpy_inf_data_key =
+ register_inferior_data_with_cleanup (NULL, py_free_inferior);
+}
+
int
gdbpy_initialize_inferior (void)
{
(PyObject *) &inferior_object_type) < 0)
return -1;
- infpy_inf_data_key =
- register_inferior_data_with_cleanup (NULL, py_free_inferior);
-
- gdb::observers::new_thread.attach (add_thread_object);
- gdb::observers::thread_exit.attach (delete_thread_object);
- gdb::observers::normal_stop.attach (python_on_normal_stop);
- gdb::observers::target_resumed.attach (python_on_resume);
- gdb::observers::inferior_call_pre.attach (python_on_inferior_call_pre);
- gdb::observers::inferior_call_post.attach (python_on_inferior_call_post);
- gdb::observers::memory_changed.attach (python_on_memory_change);
- gdb::observers::register_changed.attach (python_on_register_change);
- gdb::observers::inferior_exit.attach (python_inferior_exit);
- gdb::observers::new_objfile.attach (python_new_objfile);
- gdb::observers::inferior_added.attach (python_new_inferior);
- gdb::observers::inferior_removed.attach (python_inferior_deleted);
+ gdb::observers::new_thread.attach (add_thread_object, "py-inferior");
+ gdb::observers::thread_exit.attach (delete_thread_object, "py-inferior");
+ gdb::observers::normal_stop.attach (python_on_normal_stop, "py-inferior");
+ gdb::observers::target_resumed.attach (python_on_resume, "py-inferior");
+ gdb::observers::inferior_call_pre.attach (python_on_inferior_call_pre,
+ "py-inferior");
+ gdb::observers::inferior_call_post.attach (python_on_inferior_call_post,
+ "py-inferior");
+ gdb::observers::memory_changed.attach (python_on_memory_change,
+ "py-inferior");
+ gdb::observers::register_changed.attach (python_on_register_change,
+ "py-inferior");
+ gdb::observers::inferior_exit.attach (python_inferior_exit, "py-inferior");
+ /* Need to run after auto-load's new_objfile observer, so that
+ auto-loaded pretty-printers are available. */
+ gdb::observers::new_objfile.attach
+ (python_new_objfile, "py-inferior",
+ { &auto_load_new_objfile_observer_token });
+ gdb::observers::inferior_added.attach (python_new_inferior, "py-inferior");
+ gdb::observers::inferior_removed.attach (python_inferior_deleted,
+ "py-inferior");
membuf_object_type.tp_new = PyType_GenericNew;
if (PyType_Ready (&membuf_object_type) < 0)
static gdb_PyGetSetDef inferior_object_getset[] =
{
{ "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL },
+ { "connection_num", infpy_get_connection_num, NULL,
+ "ID of inferior's connection, as assigned by GDB.", NULL },
{ "pid", infpy_get_pid, NULL, "PID of inferior, as assigned by the OS.",
NULL },
{ "was_attached", infpy_get_was_attached, NULL,