Remove Python 2.4 and 2.5 support
[deliverable/binutils-gdb.git] / gdb / python / py-inferior.c
index d199aaa1f8c0598de3abe5bd5a5d482bf1c9eb8a..72fbf6d90b93f07ecff987e9d174d0cc15a10408 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to inferiors.
 
-   Copyright (C) 2009-2018 Free Software Foundation, Inc.
+   Copyright (C) 2009-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -26,7 +26,7 @@
 #include "python-internal.h"
 #include "arch-utils.h"
 #include "language.h"
-#include "gdb_signals.h"
+#include "common/gdb_signals.h"
 #include "py-event.h"
 #include "py-stopevent.h"
 
@@ -35,7 +35,7 @@ struct threadlist_entry {
   struct threadlist_entry *next;
 };
 
-typedef struct
+struct inferior_object
 {
   PyObject_HEAD
 
@@ -48,7 +48,7 @@ typedef struct
 
   /* Number of threads in the list.  */
   int nthreads;
-} inferior_object;
+};
 
 extern PyTypeObject inferior_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object");
@@ -86,8 +86,8 @@ python_on_normal_stop (struct bpstats *bs, int print_frame)
   if (!gdb_python_initialized)
     return;
 
-  if (!find_thread_ptid (inferior_ptid))
-      return;
+  if (inferior_ptid == null_ptid)
+    return;
 
   stop_signal = inferior_thread ()->suspend.stop_signal;
 
@@ -207,7 +207,8 @@ python_new_objfile (struct objfile *objfile)
    representing INFERIOR.  If the object has already been created,
    return it and increment the reference count,  otherwise, create it.
    Return NULL on failure.  */
-PyObject *
+
+gdbpy_ref<inferior_object>
 inferior_to_inferior_object (struct inferior *inferior)
 {
   inferior_object *inf_obj;
@@ -217,7 +218,7 @@ inferior_to_inferior_object (struct inferior *inferior)
     {
       inf_obj = PyObject_New (inferior_object, &inferior_object_type);
       if (!inf_obj)
-         return NULL;
+       return NULL;
 
       inf_obj->inferior = inferior;
       inf_obj->threads = NULL;
@@ -226,13 +227,11 @@ inferior_to_inferior_object (struct inferior *inferior)
       /* PyObject_New initializes the new object with a refcount of 1.  This
         counts for the reference we are keeping in the inferior data.  */
       set_inferior_data (inferior, infpy_inf_data_key, inf_obj);
-
     }
 
   /* We are returning a new reference.  */
-  Py_INCREF ((PyObject *)inf_obj);
-
-  return (PyObject *) inf_obj;
+  gdb_assert (inf_obj != nullptr);
+  return gdbpy_ref<inferior_object>::new_reference (inf_obj);
 }
 
 /* Called when a new inferior is created.  Notifies any Python event
@@ -248,16 +247,17 @@ python_new_inferior (struct inferior *inf)
   if (evregpy_no_listeners_p (gdb_py_events.new_inferior))
     return;
 
-  gdbpy_ref<> inf_obj (inferior_to_inferior_object (inf));
+  gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
   if (inf_obj == NULL)
     {
       gdbpy_print_stack ();
       return;
     }
 
-  gdbpy_ref<> event (create_event_object (&new_inferior_event_object_type));
+  gdbpy_ref<> event = create_event_object (&new_inferior_event_object_type);
   if (event == NULL
-      || evpy_add_attribute (event.get (), "inferior", inf_obj.get ()) < 0
+      || evpy_add_attribute (event.get (), "inferior",
+                            (PyObject *) inf_obj.get ()) < 0
       || evpy_emit_event (event.get (), gdb_py_events.new_inferior) < 0)
     gdbpy_print_stack ();
 }
@@ -275,53 +275,36 @@ python_inferior_deleted (struct inferior *inf)
   if (evregpy_no_listeners_p (gdb_py_events.inferior_deleted))
     return;
 
-  gdbpy_ref<> inf_obj (inferior_to_inferior_object (inf));
+  gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
   if (inf_obj == NULL)
     {
       gdbpy_print_stack ();
       return;
     }
 
-  gdbpy_ref<> event (create_event_object (&inferior_deleted_event_object_type));
+  gdbpy_ref<> event = create_event_object (&inferior_deleted_event_object_type);
   if (event == NULL
-      || evpy_add_attribute (event.get (), "inferior", inf_obj.get ()) < 0
+      || evpy_add_attribute (event.get (), "inferior",
+                            (PyObject *) inf_obj.get ()) < 0
       || evpy_emit_event (event.get (), gdb_py_events.inferior_deleted) < 0)
     gdbpy_print_stack ();
 }
 
-/* Finds the Python Inferior object for the given PID.  Returns a
-   reference, or NULL if PID does not match any inferior object. */
-
-PyObject *
-find_inferior_object (int pid)
-{
-  struct inferior *inf = find_inferior_pid (pid);
-
-  if (inf)
-    return inferior_to_inferior_object (inf);
-
-  return NULL;
-}
-
-thread_object *
-find_thread_object (ptid_t ptid)
+gdbpy_ref<>
+thread_to_thread_object (thread_info *thr)
 {
-  int pid;
-  struct threadlist_entry *thread;
-
-  pid = ptid_get_pid (ptid);
-  if (pid == 0)
-    return NULL;
-
-  gdbpy_ref<> inf_obj (find_inferior_object (pid));
+  gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (thr->inf);
   if (inf_obj == NULL)
     return NULL;
 
-  for (thread = ((inferior_object *)(inf_obj.get ()))->threads; thread;
+  for (threadlist_entry *thread = inf_obj->threads;
+       thread != NULL;
        thread = thread->next)
-    if (ptid_equal (thread->thread_obj->thread->ptid, ptid))
-      return thread->thread_obj;
+    if (thread->thread_obj->thread == thr)
+      return gdbpy_ref<>::new_reference ((PyObject *) thread->thread_obj);
 
+  PyErr_SetString (PyExc_SystemError,
+                  _("could not find gdb thread object"));
   return NULL;
 }
 
@@ -356,8 +339,8 @@ add_thread_object (struct thread_info *tp)
   if (evregpy_no_listeners_p (gdb_py_events.new_thread))
     return;
 
-  gdbpy_ref<> event (create_thread_event_object (&new_thread_event_object_type,
-                                                (PyObject *) thread_obj));
+  gdbpy_ref<> event create_thread_event_object (&new_thread_event_object_type,
+                                                 (PyObject *) thread_obj);
   if (event == NULL
       || evpy_emit_event (event.get (), gdb_py_events.new_thread) < 0)
     gdbpy_print_stack ();
@@ -373,8 +356,7 @@ delete_thread_object (struct thread_info *tp, int ignore)
 
   gdbpy_enter enter_py (python_gdbarch, python_language);
 
-  gdbpy_ref<inferior_object> inf_obj
-    ((inferior_object *) find_inferior_object (ptid_get_pid (tp->ptid)));
+  gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
   if (inf_obj == NULL)
     return;
 
@@ -462,16 +444,31 @@ infpy_get_was_attached (PyObject *self, void *closure)
   Py_RETURN_FALSE;
 }
 
+/* Getter of gdb.Inferior.progspace.  */
+
+static PyObject *
+infpy_get_progspace (PyObject *self, void *closure)
+{
+  inferior_object *inf = (inferior_object *) self;
+
+  INFPY_REQUIRE_VALID (inf);
+
+  program_space *pspace = inf->inferior->pspace;
+  gdb_assert (pspace != nullptr);
+
+  return pspace_to_pspace_object (pspace).release ();
+}
+
 static int
 build_inferior_list (struct inferior *inf, void *arg)
 {
   PyObject *list = (PyObject *) arg;
-  gdbpy_ref<> inferior (inferior_to_inferior_object (inf));
+  gdbpy_ref<inferior_object> inferior = inferior_to_inferior_object (inf);
 
-  if (inferior  == NULL)
+  if (inferior == NULL)
     return 0;
 
-  return PyList_Append (list, inferior.get ()) ? 1 : 0;
+  return PyList_Append (list, (PyObject *) inferior.get ()) ? 1 : 0;
 }
 
 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
@@ -499,7 +496,7 @@ static PyObject *
 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
 {
   CORE_ADDR addr, length;
-  gdb_byte *buffer = NULL;
+  gdb::unique_xmalloc_ptr<gdb_byte> buffer;
   PyObject *addr_obj, *length_obj, *result;
   static const char *keywords[] = { "address", "length", NULL };
 
@@ -513,13 +510,12 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
 
   TRY
     {
-      buffer = (gdb_byte *) xmalloc (length);
+      buffer.reset ((gdb_byte *) xmalloc (length));
 
-      read_memory (addr, buffer, length);
+      read_memory (addr, buffer.get (), length);
     }
   CATCH (except, RETURN_MASK_ALL)
     {
-      xfree (buffer);
       GDB_PY_HANDLE_EXCEPTION (except);
     }
   END_CATCH
@@ -527,12 +523,9 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
   gdbpy_ref<membuf_object> membuf_obj (PyObject_New (membuf_object,
                                                     &membuf_object_type));
   if (membuf_obj == NULL)
-    {
-      xfree (buffer);
-      return NULL;
-    }
+    return NULL;
 
-  membuf_obj->buffer = buffer;
+  membuf_obj->buffer = buffer.release ();
   membuf_obj->addr = addr;
   membuf_obj->length = length;
 
@@ -561,31 +554,23 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
   CORE_ADDR addr, length;
   PyObject *addr_obj, *length_obj = NULL;
   static const char *keywords[] = { "address", "buffer", "length", NULL };
-#ifdef IS_PY3K
   Py_buffer pybuf;
 
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords,
                                        &addr_obj, &pybuf, &length_obj))
     return NULL;
 
+  Py_buffer_up buffer_up (&pybuf);
   buffer = (const gdb_byte *) pybuf.buf;
   buf_len = pybuf.len;
-#else
-  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
-                                       &addr_obj, &buffer, &buf_len,
-                                       &length_obj))
-    return NULL;
-
-  buffer = (const gdb_byte *) buffer;
-#endif
 
   if (get_addr_from_python (addr_obj, &addr) < 0)
-    goto fail;
+    return nullptr;
 
   if (!length_obj)
     length = buf_len;
   else if (get_addr_from_python (length_obj, &length) < 0)
-    goto fail;
+    return nullptr;
 
   TRY
     {
@@ -597,18 +582,9 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
     }
   END_CATCH
 
-#ifdef IS_PY3K
-  PyBuffer_Release (&pybuf);
-#endif
   GDB_PY_HANDLE_EXCEPTION (except);
 
   Py_RETURN_NONE;
-
- fail:
-#ifdef IS_PY3K
-  PyBuffer_Release (&pybuf);
-#endif
-  return NULL;
 }
 
 /* Destructor of Membuf objects.  */
@@ -717,7 +693,6 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
   const gdb_byte *buffer;
   CORE_ADDR found_addr;
   int found = 0;
-#ifdef IS_PY3K
   Py_buffer pybuf;
 
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords,
@@ -725,42 +700,21 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
                                        &pybuf))
     return NULL;
 
+  Py_buffer_up buffer_up (&pybuf);
   buffer = (const gdb_byte *) pybuf.buf;
   pattern_size = pybuf.len;
-#else
-  PyObject *pattern;
-  const void *vbuffer;
-
-  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
-                                       &start_addr_obj, &length_obj,
-                                       &pattern))
-     return NULL;
-
-  if (!PyObject_CheckReadBuffer (pattern))
-    {
-      PyErr_SetString (PyExc_RuntimeError,
-                      _("The pattern is not a Python buffer."));
-
-      return NULL;
-    }
-
-  if (PyObject_AsReadBuffer (pattern, &vbuffer, &pattern_size) == -1)
-    return NULL;
-
-  buffer = (const gdb_byte *) vbuffer;
-#endif
 
   if (get_addr_from_python (start_addr_obj, &start_addr) < 0)
-    goto fail;
+    return nullptr;
 
   if (get_addr_from_python (length_obj, &length) < 0)
-    goto fail;
+    return nullptr;
 
   if (!length)
     {
       PyErr_SetString (PyExc_ValueError,
                       _("Search range is empty."));
-      goto fail;
+      return nullptr;
     }
   /* Watch for overflows.  */
   else if (length > CORE_ADDR_MAX
@@ -768,7 +722,7 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
     {
       PyErr_SetString (PyExc_ValueError,
                       _("The search range is too large."));
-      goto fail;
+      return nullptr;
     }
 
   TRY
@@ -783,21 +737,12 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
     }
   END_CATCH
 
-#ifdef IS_PY3K
-  PyBuffer_Release (&pybuf);
-#endif
   GDB_PY_HANDLE_EXCEPTION (except);
 
   if (found)
     return PyLong_FromLong (found_addr);
   else
     Py_RETURN_NONE;
-
- fail:
-#ifdef IS_PY3K
-  PyBuffer_Release (&pybuf);
-#endif
-  return NULL;
 }
 
 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
@@ -817,10 +762,10 @@ infpy_is_valid (PyObject *self, PyObject *args)
 /* Implementation of gdb.Inferior.thread_from_thread_handle (self, handle)
                         ->  gdb.InferiorThread.  */
 
-PyObject *
+static PyObject *
 infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw)
 {
-  PyObject *handle_obj, *result;
+  PyObject *handle_obj;
   inferior_object *inf_obj = (inferior_object *) self;
   static const char *keywords[] = { "thread_handle", NULL };
 
@@ -829,8 +774,6 @@ infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw)
   if (! gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, &handle_obj))
     return NULL;
 
-  result = Py_None;
-
   if (!gdbpy_is_value_object (handle_obj))
     {
       PyErr_SetString (PyExc_TypeError,
@@ -838,29 +781,50 @@ infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw)
 
       return NULL;
     }
-  else
+
+  TRY
     {
-      TRY
-       {
-         struct thread_info *thread_info;
-         struct value *val = value_object_to_value (handle_obj);
-
-         thread_info = find_thread_by_handle (val, inf_obj->inferior);
-         if (thread_info != NULL)
-           {
-             result = (PyObject *) find_thread_object (thread_info->ptid);
-             if (result != NULL)
-               Py_INCREF (result);
-           }
-       }
-      CATCH (except, RETURN_MASK_ALL)
-       {
-         GDB_PY_HANDLE_EXCEPTION (except);
-       }
-      END_CATCH
+      struct thread_info *thread_info;
+      struct value *val = value_object_to_value (handle_obj);
+
+      thread_info = find_thread_by_handle (val, inf_obj->inferior);
+      if (thread_info != NULL)
+       return thread_to_thread_object (thread_info).release ();
     }
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
 
-  return result;
+  Py_RETURN_NONE;
+}
+
+/* Implementation of gdb.Inferior.architecture.  */
+
+static PyObject *
+infpy_architecture (PyObject *self, PyObject *args)
+{
+  inferior_object *inf = (inferior_object *) self;
+
+  INFPY_REQUIRE_VALID (inf);
+
+  return gdbarch_to_arch_object (inf->inferior->gdbarch);
+}
+
+/* Implement repr() for gdb.Inferior.  */
+
+static PyObject *
+infpy_repr (PyObject *obj)
+{
+  inferior_object *self = (inferior_object *) obj;
+  inferior *inf = self->inferior;
+
+  if (inf == nullptr)
+    return PyString_FromString ("<gdb.Inferior (invalid)>");
+
+  return PyString_FromFormat ("<gdb.Inferior num=%d, pid=%d>",
+                             inf->num, inf->pid);
 }
 
 
@@ -910,7 +874,8 @@ py_free_inferior (struct inferior *inf, void *datum)
 PyObject *
 gdbpy_selected_inferior (PyObject *self, PyObject *args)
 {
-  return inferior_to_inferior_object (current_inferior ());
+  return ((PyObject *)
+         inferior_to_inferior_object (current_inferior ()).release ());
 }
 
 int
@@ -943,8 +908,8 @@ gdbpy_initialize_inferior (void)
   if (PyType_Ready (&membuf_object_type) < 0)
     return -1;
 
-  return gdb_pymodule_addobject (gdb_module, "Membuf", (PyObject *)
-                                &membuf_object_type);
+  return gdb_pymodule_addobject (gdb_module, "Membuf",
+                                (PyObject *) &membuf_object_type);
 }
 
 static gdb_PyGetSetDef inferior_object_getset[] =
@@ -954,6 +919,7 @@ static gdb_PyGetSetDef inferior_object_getset[] =
     NULL },
   { "was_attached", infpy_get_was_attached, NULL,
     "True if the inferior was created using 'attach'.", NULL },
+  { "progspace", infpy_get_progspace, NULL, "Program space of this inferior" },
   { NULL }
 };
 
@@ -980,6 +946,9 @@ Return a long with the address of a match, or None." },
     METH_VARARGS | METH_KEYWORDS,
     "thread_from_thread_handle (handle) -> gdb.InferiorThread.\n\
 Return thread object corresponding to thread handle." },
+  { "architecture", (PyCFunction) infpy_architecture, METH_NOARGS,
+    "architecture () -> gdb.Architecture\n\
+Return architecture of this inferior." },
   { NULL }
 };
 
@@ -994,7 +963,7 @@ PyTypeObject inferior_object_type =
   0,                             /* tp_getattr */
   0,                             /* tp_setattr */
   0,                             /* tp_compare */
-  0,                             /* tp_repr */
+  infpy_repr,                    /* tp_repr */
   0,                             /* tp_as_number */
   0,                             /* tp_as_sequence */
   0,                             /* tp_as_mapping */
@@ -1033,20 +1002,11 @@ static PyBufferProcs buffer_procs =
 
 #else
 
-/* Python doesn't provide a decent way to get compatibility here.  */
-#if HAVE_LIBPYTHON2_4
-#define CHARBUFFERPROC_NAME getcharbufferproc
-#else
-#define CHARBUFFERPROC_NAME charbufferproc
-#endif
-
 static PyBufferProcs buffer_procs = {
   get_read_buffer,
   get_write_buffer,
   get_seg_count,
-  /* The cast here works around a difference between Python 2.4 and
-     Python 2.5.  */
-  (CHARBUFFERPROC_NAME) get_char_buffer
+  get_char_buffer
 };
 #endif /* IS_PY3K */
 
This page took 0.033615 seconds and 4 git commands to generate.