Home | History | Annotate | Line # | Download | only in python
py-inferior.c revision 1.1
      1  1.1  christos /* Python interface to inferiors.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 2009-2014 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "exceptions.h"
     22  1.1  christos #include "gdbcore.h"
     23  1.1  christos #include "gdbthread.h"
     24  1.1  christos #include "inferior.h"
     25  1.1  christos #include "objfiles.h"
     26  1.1  christos #include "observer.h"
     27  1.1  christos #include "python-internal.h"
     28  1.1  christos #include "arch-utils.h"
     29  1.1  christos #include "language.h"
     30  1.1  christos #include "gdb_signals.h"
     31  1.1  christos #include "py-event.h"
     32  1.1  christos #include "py-stopevent.h"
     33  1.1  christos 
     34  1.1  christos struct threadlist_entry {
     35  1.1  christos   thread_object *thread_obj;
     36  1.1  christos   struct threadlist_entry *next;
     37  1.1  christos };
     38  1.1  christos 
     39  1.1  christos typedef struct
     40  1.1  christos {
     41  1.1  christos   PyObject_HEAD
     42  1.1  christos 
     43  1.1  christos   /* The inferior we represent.  */
     44  1.1  christos   struct inferior *inferior;
     45  1.1  christos 
     46  1.1  christos   /* thread_object instances under this inferior.  This list owns a
     47  1.1  christos      reference to each object it contains.  */
     48  1.1  christos   struct threadlist_entry *threads;
     49  1.1  christos 
     50  1.1  christos   /* Number of threads in the list.  */
     51  1.1  christos   int nthreads;
     52  1.1  christos } inferior_object;
     53  1.1  christos 
     54  1.1  christos static PyTypeObject inferior_object_type
     55  1.1  christos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object");
     56  1.1  christos 
     57  1.1  christos static const struct inferior_data *infpy_inf_data_key;
     58  1.1  christos 
     59  1.1  christos typedef struct {
     60  1.1  christos   PyObject_HEAD
     61  1.1  christos   void *buffer;
     62  1.1  christos 
     63  1.1  christos   /* These are kept just for mbpy_str.  */
     64  1.1  christos   CORE_ADDR addr;
     65  1.1  christos   CORE_ADDR length;
     66  1.1  christos } membuf_object;
     67  1.1  christos 
     68  1.1  christos static PyTypeObject membuf_object_type
     69  1.1  christos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("membuf_object");
     70  1.1  christos 
     71  1.1  christos /* Require that INFERIOR be a valid inferior ID.  */
     72  1.1  christos #define INFPY_REQUIRE_VALID(Inferior)				\
     73  1.1  christos   do {								\
     74  1.1  christos     if (!Inferior->inferior)					\
     75  1.1  christos       {								\
     76  1.1  christos 	PyErr_SetString (PyExc_RuntimeError,			\
     77  1.1  christos 			 _("Inferior no longer exists."));	\
     78  1.1  christos 	return NULL;						\
     79  1.1  christos       }								\
     80  1.1  christos   } while (0)
     81  1.1  christos 
     82  1.1  christos static void
     83  1.1  christos python_on_normal_stop (struct bpstats *bs, int print_frame)
     84  1.1  christos {
     85  1.1  christos   struct cleanup *cleanup;
     86  1.1  christos   enum gdb_signal stop_signal;
     87  1.1  christos 
     88  1.1  christos   if (!gdb_python_initialized)
     89  1.1  christos     return;
     90  1.1  christos 
     91  1.1  christos   if (!find_thread_ptid (inferior_ptid))
     92  1.1  christos       return;
     93  1.1  christos 
     94  1.1  christos   stop_signal = inferior_thread ()->suspend.stop_signal;
     95  1.1  christos 
     96  1.1  christos   cleanup = ensure_python_env (get_current_arch (), current_language);
     97  1.1  christos 
     98  1.1  christos   if (emit_stop_event (bs, stop_signal) < 0)
     99  1.1  christos     gdbpy_print_stack ();
    100  1.1  christos 
    101  1.1  christos   do_cleanups (cleanup);
    102  1.1  christos }
    103  1.1  christos 
    104  1.1  christos static void
    105  1.1  christos python_on_resume (ptid_t ptid)
    106  1.1  christos {
    107  1.1  christos   struct cleanup *cleanup;
    108  1.1  christos 
    109  1.1  christos   if (!gdb_python_initialized)
    110  1.1  christos     return;
    111  1.1  christos 
    112  1.1  christos   cleanup = ensure_python_env (target_gdbarch (), current_language);
    113  1.1  christos 
    114  1.1  christos   if (emit_continue_event (ptid) < 0)
    115  1.1  christos     gdbpy_print_stack ();
    116  1.1  christos 
    117  1.1  christos   do_cleanups (cleanup);
    118  1.1  christos }
    119  1.1  christos 
    120  1.1  christos static void
    121  1.1  christos python_inferior_exit (struct inferior *inf)
    122  1.1  christos {
    123  1.1  christos   struct cleanup *cleanup;
    124  1.1  christos   const LONGEST *exit_code = NULL;
    125  1.1  christos 
    126  1.1  christos   if (!gdb_python_initialized)
    127  1.1  christos     return;
    128  1.1  christos 
    129  1.1  christos   cleanup = ensure_python_env (target_gdbarch (), current_language);
    130  1.1  christos 
    131  1.1  christos   if (inf->has_exit_code)
    132  1.1  christos     exit_code = &inf->exit_code;
    133  1.1  christos 
    134  1.1  christos   if (emit_exited_event (exit_code, inf) < 0)
    135  1.1  christos     gdbpy_print_stack ();
    136  1.1  christos 
    137  1.1  christos   do_cleanups (cleanup);
    138  1.1  christos }
    139  1.1  christos 
    140  1.1  christos /* Callback used to notify Python listeners about new objfiles loaded in the
    141  1.1  christos    inferior.  */
    142  1.1  christos 
    143  1.1  christos static void
    144  1.1  christos python_new_objfile (struct objfile *objfile)
    145  1.1  christos {
    146  1.1  christos   struct cleanup *cleanup;
    147  1.1  christos 
    148  1.1  christos   if (objfile == NULL)
    149  1.1  christos     return;
    150  1.1  christos 
    151  1.1  christos   if (!gdb_python_initialized)
    152  1.1  christos     return;
    153  1.1  christos 
    154  1.1  christos   cleanup = ensure_python_env (get_objfile_arch (objfile), current_language);
    155  1.1  christos 
    156  1.1  christos   if (emit_new_objfile_event (objfile) < 0)
    157  1.1  christos     gdbpy_print_stack ();
    158  1.1  christos 
    159  1.1  christos   do_cleanups (cleanup);
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos /* Return a reference to the Python object of type Inferior
    163  1.1  christos    representing INFERIOR.  If the object has already been created,
    164  1.1  christos    return it and increment the reference count,  otherwise, create it.
    165  1.1  christos    Return NULL on failure.  */
    166  1.1  christos PyObject *
    167  1.1  christos inferior_to_inferior_object (struct inferior *inferior)
    168  1.1  christos {
    169  1.1  christos   inferior_object *inf_obj;
    170  1.1  christos 
    171  1.1  christos   inf_obj = inferior_data (inferior, infpy_inf_data_key);
    172  1.1  christos   if (!inf_obj)
    173  1.1  christos     {
    174  1.1  christos       inf_obj = PyObject_New (inferior_object, &inferior_object_type);
    175  1.1  christos       if (!inf_obj)
    176  1.1  christos 	  return NULL;
    177  1.1  christos 
    178  1.1  christos       inf_obj->inferior = inferior;
    179  1.1  christos       inf_obj->threads = NULL;
    180  1.1  christos       inf_obj->nthreads = 0;
    181  1.1  christos 
    182  1.1  christos       set_inferior_data (inferior, infpy_inf_data_key, inf_obj);
    183  1.1  christos 
    184  1.1  christos     }
    185  1.1  christos   else
    186  1.1  christos     Py_INCREF ((PyObject *)inf_obj);
    187  1.1  christos 
    188  1.1  christos   return (PyObject *) inf_obj;
    189  1.1  christos }
    190  1.1  christos 
    191  1.1  christos /* Finds the Python Inferior object for the given PID.  Returns a
    192  1.1  christos    reference, or NULL if PID does not match any inferior object. */
    193  1.1  christos 
    194  1.1  christos PyObject *
    195  1.1  christos find_inferior_object (int pid)
    196  1.1  christos {
    197  1.1  christos   struct inferior *inf = find_inferior_pid (pid);
    198  1.1  christos 
    199  1.1  christos   if (inf)
    200  1.1  christos     return inferior_to_inferior_object (inf);
    201  1.1  christos 
    202  1.1  christos   return NULL;
    203  1.1  christos }
    204  1.1  christos 
    205  1.1  christos thread_object *
    206  1.1  christos find_thread_object (ptid_t ptid)
    207  1.1  christos {
    208  1.1  christos   int pid;
    209  1.1  christos   struct threadlist_entry *thread;
    210  1.1  christos   PyObject *inf_obj;
    211  1.1  christos   thread_object *found = NULL;
    212  1.1  christos 
    213  1.1  christos   pid = ptid_get_pid (ptid);
    214  1.1  christos   if (pid == 0)
    215  1.1  christos     return NULL;
    216  1.1  christos 
    217  1.1  christos   inf_obj = find_inferior_object (pid);
    218  1.1  christos 
    219  1.1  christos   if (! inf_obj)
    220  1.1  christos     return NULL;
    221  1.1  christos 
    222  1.1  christos   for (thread = ((inferior_object *)inf_obj)->threads; thread;
    223  1.1  christos        thread = thread->next)
    224  1.1  christos     if (ptid_equal (thread->thread_obj->thread->ptid, ptid))
    225  1.1  christos       {
    226  1.1  christos 	found = thread->thread_obj;
    227  1.1  christos 	break;
    228  1.1  christos       }
    229  1.1  christos 
    230  1.1  christos   Py_DECREF (inf_obj);
    231  1.1  christos 
    232  1.1  christos   if (found)
    233  1.1  christos     return found;
    234  1.1  christos 
    235  1.1  christos   return NULL;
    236  1.1  christos }
    237  1.1  christos 
    238  1.1  christos static void
    239  1.1  christos add_thread_object (struct thread_info *tp)
    240  1.1  christos {
    241  1.1  christos   struct cleanup *cleanup;
    242  1.1  christos   thread_object *thread_obj;
    243  1.1  christos   inferior_object *inf_obj;
    244  1.1  christos   struct threadlist_entry *entry;
    245  1.1  christos 
    246  1.1  christos   if (!gdb_python_initialized)
    247  1.1  christos     return;
    248  1.1  christos 
    249  1.1  christos   cleanup = ensure_python_env (python_gdbarch, python_language);
    250  1.1  christos 
    251  1.1  christos   thread_obj = create_thread_object (tp);
    252  1.1  christos   if (!thread_obj)
    253  1.1  christos     {
    254  1.1  christos       gdbpy_print_stack ();
    255  1.1  christos       do_cleanups (cleanup);
    256  1.1  christos       return;
    257  1.1  christos     }
    258  1.1  christos 
    259  1.1  christos   inf_obj = (inferior_object *) thread_obj->inf_obj;
    260  1.1  christos 
    261  1.1  christos   entry = xmalloc (sizeof (struct threadlist_entry));
    262  1.1  christos   entry->thread_obj = thread_obj;
    263  1.1  christos   entry->next = inf_obj->threads;
    264  1.1  christos 
    265  1.1  christos   inf_obj->threads = entry;
    266  1.1  christos   inf_obj->nthreads++;
    267  1.1  christos 
    268  1.1  christos   do_cleanups (cleanup);
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos static void
    272  1.1  christos delete_thread_object (struct thread_info *tp, int ignore)
    273  1.1  christos {
    274  1.1  christos   struct cleanup *cleanup;
    275  1.1  christos   inferior_object *inf_obj;
    276  1.1  christos   struct threadlist_entry **entry, *tmp;
    277  1.1  christos 
    278  1.1  christos   if (!gdb_python_initialized)
    279  1.1  christos     return;
    280  1.1  christos 
    281  1.1  christos   cleanup = ensure_python_env (python_gdbarch, python_language);
    282  1.1  christos 
    283  1.1  christos   inf_obj
    284  1.1  christos     = (inferior_object *) find_inferior_object (ptid_get_pid (tp->ptid));
    285  1.1  christos   if (!inf_obj)
    286  1.1  christos     {
    287  1.1  christos       do_cleanups (cleanup);
    288  1.1  christos       return;
    289  1.1  christos     }
    290  1.1  christos 
    291  1.1  christos   /* Find thread entry in its inferior's thread_list.  */
    292  1.1  christos   for (entry = &inf_obj->threads; *entry != NULL; entry =
    293  1.1  christos 	 &(*entry)->next)
    294  1.1  christos     if ((*entry)->thread_obj->thread == tp)
    295  1.1  christos       break;
    296  1.1  christos 
    297  1.1  christos   if (!*entry)
    298  1.1  christos     {
    299  1.1  christos       Py_DECREF (inf_obj);
    300  1.1  christos       do_cleanups (cleanup);
    301  1.1  christos       return;
    302  1.1  christos     }
    303  1.1  christos 
    304  1.1  christos   tmp = *entry;
    305  1.1  christos   tmp->thread_obj->thread = NULL;
    306  1.1  christos 
    307  1.1  christos   *entry = (*entry)->next;
    308  1.1  christos   inf_obj->nthreads--;
    309  1.1  christos 
    310  1.1  christos   Py_DECREF (tmp->thread_obj);
    311  1.1  christos   Py_DECREF (inf_obj);
    312  1.1  christos   xfree (tmp);
    313  1.1  christos 
    314  1.1  christos   do_cleanups (cleanup);
    315  1.1  christos }
    316  1.1  christos 
    317  1.1  christos static PyObject *
    318  1.1  christos infpy_threads (PyObject *self, PyObject *args)
    319  1.1  christos {
    320  1.1  christos   int i;
    321  1.1  christos   struct threadlist_entry *entry;
    322  1.1  christos   inferior_object *inf_obj = (inferior_object *) self;
    323  1.1  christos   PyObject *tuple;
    324  1.1  christos   volatile struct gdb_exception except;
    325  1.1  christos 
    326  1.1  christos   INFPY_REQUIRE_VALID (inf_obj);
    327  1.1  christos 
    328  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    329  1.1  christos     update_thread_list ();
    330  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    331  1.1  christos 
    332  1.1  christos   tuple = PyTuple_New (inf_obj->nthreads);
    333  1.1  christos   if (!tuple)
    334  1.1  christos     return NULL;
    335  1.1  christos 
    336  1.1  christos   for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads;
    337  1.1  christos        i++, entry = entry->next)
    338  1.1  christos     {
    339  1.1  christos       Py_INCREF (entry->thread_obj);
    340  1.1  christos       PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj);
    341  1.1  christos     }
    342  1.1  christos 
    343  1.1  christos   return tuple;
    344  1.1  christos }
    345  1.1  christos 
    346  1.1  christos static PyObject *
    347  1.1  christos infpy_get_num (PyObject *self, void *closure)
    348  1.1  christos {
    349  1.1  christos   inferior_object *inf = (inferior_object *) self;
    350  1.1  christos 
    351  1.1  christos   INFPY_REQUIRE_VALID (inf);
    352  1.1  christos 
    353  1.1  christos   return PyLong_FromLong (inf->inferior->num);
    354  1.1  christos }
    355  1.1  christos 
    356  1.1  christos static PyObject *
    357  1.1  christos infpy_get_pid (PyObject *self, void *closure)
    358  1.1  christos {
    359  1.1  christos   inferior_object *inf = (inferior_object *) self;
    360  1.1  christos 
    361  1.1  christos   INFPY_REQUIRE_VALID (inf);
    362  1.1  christos 
    363  1.1  christos   return PyLong_FromLong (inf->inferior->pid);
    364  1.1  christos }
    365  1.1  christos 
    366  1.1  christos static PyObject *
    367  1.1  christos infpy_get_was_attached (PyObject *self, void *closure)
    368  1.1  christos {
    369  1.1  christos   inferior_object *inf = (inferior_object *) self;
    370  1.1  christos 
    371  1.1  christos   INFPY_REQUIRE_VALID (inf);
    372  1.1  christos   if (inf->inferior->attach_flag)
    373  1.1  christos     Py_RETURN_TRUE;
    374  1.1  christos   Py_RETURN_FALSE;
    375  1.1  christos }
    376  1.1  christos 
    377  1.1  christos static int
    378  1.1  christos build_inferior_list (struct inferior *inf, void *arg)
    379  1.1  christos {
    380  1.1  christos   PyObject *list = arg;
    381  1.1  christos   PyObject *inferior = inferior_to_inferior_object (inf);
    382  1.1  christos   int success = 0;
    383  1.1  christos 
    384  1.1  christos   if (! inferior)
    385  1.1  christos     return 0;
    386  1.1  christos 
    387  1.1  christos   success = PyList_Append (list, inferior);
    388  1.1  christos   Py_DECREF (inferior);
    389  1.1  christos 
    390  1.1  christos   if (success)
    391  1.1  christos     return 1;
    392  1.1  christos 
    393  1.1  christos   return 0;
    394  1.1  christos }
    395  1.1  christos 
    396  1.1  christos /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
    397  1.1  christos    Returns a tuple of all inferiors.  */
    398  1.1  christos PyObject *
    399  1.1  christos gdbpy_inferiors (PyObject *unused, PyObject *unused2)
    400  1.1  christos {
    401  1.1  christos   PyObject *list, *tuple;
    402  1.1  christos 
    403  1.1  christos   list = PyList_New (0);
    404  1.1  christos   if (!list)
    405  1.1  christos     return NULL;
    406  1.1  christos 
    407  1.1  christos   if (iterate_over_inferiors (build_inferior_list, list))
    408  1.1  christos     {
    409  1.1  christos       Py_DECREF (list);
    410  1.1  christos       return NULL;
    411  1.1  christos     }
    412  1.1  christos 
    413  1.1  christos   tuple = PyList_AsTuple (list);
    414  1.1  christos   Py_DECREF (list);
    415  1.1  christos 
    416  1.1  christos   return tuple;
    417  1.1  christos }
    418  1.1  christos 
    419  1.1  christos /* Membuf and memory manipulation.  */
    420  1.1  christos 
    421  1.1  christos /* Implementation of Inferior.read_memory (address, length).
    422  1.1  christos    Returns a Python buffer object with LENGTH bytes of the inferior's
    423  1.1  christos    memory at ADDRESS.  Both arguments are integers.  Returns NULL on error,
    424  1.1  christos    with a python exception set.  */
    425  1.1  christos static PyObject *
    426  1.1  christos infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
    427  1.1  christos {
    428  1.1  christos   CORE_ADDR addr, length;
    429  1.1  christos   void *buffer = NULL;
    430  1.1  christos   membuf_object *membuf_obj;
    431  1.1  christos   PyObject *addr_obj, *length_obj, *result;
    432  1.1  christos   volatile struct gdb_exception except;
    433  1.1  christos   static char *keywords[] = { "address", "length", NULL };
    434  1.1  christos 
    435  1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
    436  1.1  christos 				     &addr_obj, &length_obj))
    437  1.1  christos     return NULL;
    438  1.1  christos 
    439  1.1  christos   if (get_addr_from_python (addr_obj, &addr) < 0
    440  1.1  christos       || get_addr_from_python (length_obj, &length) < 0)
    441  1.1  christos     return NULL;
    442  1.1  christos 
    443  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    444  1.1  christos     {
    445  1.1  christos       buffer = xmalloc (length);
    446  1.1  christos 
    447  1.1  christos       read_memory (addr, buffer, length);
    448  1.1  christos     }
    449  1.1  christos   if (except.reason < 0)
    450  1.1  christos     {
    451  1.1  christos       xfree (buffer);
    452  1.1  christos       GDB_PY_HANDLE_EXCEPTION (except);
    453  1.1  christos     }
    454  1.1  christos 
    455  1.1  christos   membuf_obj = PyObject_New (membuf_object, &membuf_object_type);
    456  1.1  christos   if (membuf_obj == NULL)
    457  1.1  christos     {
    458  1.1  christos       xfree (buffer);
    459  1.1  christos       return NULL;
    460  1.1  christos     }
    461  1.1  christos 
    462  1.1  christos   membuf_obj->buffer = buffer;
    463  1.1  christos   membuf_obj->addr = addr;
    464  1.1  christos   membuf_obj->length = length;
    465  1.1  christos 
    466  1.1  christos #ifdef IS_PY3K
    467  1.1  christos   result = PyMemoryView_FromObject ((PyObject *) membuf_obj);
    468  1.1  christos #else
    469  1.1  christos   result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
    470  1.1  christos 					 Py_END_OF_BUFFER);
    471  1.1  christos #endif
    472  1.1  christos   Py_DECREF (membuf_obj);
    473  1.1  christos 
    474  1.1  christos   return result;
    475  1.1  christos }
    476  1.1  christos 
    477  1.1  christos /* Implementation of Inferior.write_memory (address, buffer [, length]).
    478  1.1  christos    Writes the contents of BUFFER (a Python object supporting the read
    479  1.1  christos    buffer protocol) at ADDRESS in the inferior's memory.  Write LENGTH
    480  1.1  christos    bytes from BUFFER, or its entire contents if the argument is not
    481  1.1  christos    provided.  The function returns nothing.  Returns NULL on error, with
    482  1.1  christos    a python exception set.  */
    483  1.1  christos static PyObject *
    484  1.1  christos infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
    485  1.1  christos {
    486  1.1  christos   Py_ssize_t buf_len;
    487  1.1  christos   const char *buffer;
    488  1.1  christos   CORE_ADDR addr, length;
    489  1.1  christos   PyObject *addr_obj, *length_obj = NULL;
    490  1.1  christos   volatile struct gdb_exception except;
    491  1.1  christos   static char *keywords[] = { "address", "buffer", "length", NULL };
    492  1.1  christos #ifdef IS_PY3K
    493  1.1  christos   Py_buffer pybuf;
    494  1.1  christos 
    495  1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords,
    496  1.1  christos 				     &addr_obj, &pybuf,
    497  1.1  christos 				     &length_obj))
    498  1.1  christos     return NULL;
    499  1.1  christos 
    500  1.1  christos   buffer = pybuf.buf;
    501  1.1  christos   buf_len = pybuf.len;
    502  1.1  christos #else
    503  1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
    504  1.1  christos 				     &addr_obj, &buffer, &buf_len,
    505  1.1  christos 				     &length_obj))
    506  1.1  christos     return NULL;
    507  1.1  christos #endif
    508  1.1  christos 
    509  1.1  christos   if (get_addr_from_python (addr_obj, &addr) < 0)
    510  1.1  christos     goto fail;
    511  1.1  christos 
    512  1.1  christos   if (!length_obj)
    513  1.1  christos     length = buf_len;
    514  1.1  christos   else if (get_addr_from_python (length_obj, &length) < 0)
    515  1.1  christos     goto fail;
    516  1.1  christos 
    517  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    518  1.1  christos     {
    519  1.1  christos       write_memory_with_notification (addr, (gdb_byte *) buffer, length);
    520  1.1  christos     }
    521  1.1  christos #ifdef IS_PY3K
    522  1.1  christos   PyBuffer_Release (&pybuf);
    523  1.1  christos #endif
    524  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    525  1.1  christos 
    526  1.1  christos   Py_RETURN_NONE;
    527  1.1  christos 
    528  1.1  christos  fail:
    529  1.1  christos #ifdef IS_PY3K
    530  1.1  christos   PyBuffer_Release (&pybuf);
    531  1.1  christos #endif
    532  1.1  christos   return NULL;
    533  1.1  christos }
    534  1.1  christos 
    535  1.1  christos /* Destructor of Membuf objects.  */
    536  1.1  christos static void
    537  1.1  christos mbpy_dealloc (PyObject *self)
    538  1.1  christos {
    539  1.1  christos   xfree (((membuf_object *) self)->buffer);
    540  1.1  christos   Py_TYPE (self)->tp_free (self);
    541  1.1  christos }
    542  1.1  christos 
    543  1.1  christos /* Return a description of the Membuf object.  */
    544  1.1  christos static PyObject *
    545  1.1  christos mbpy_str (PyObject *self)
    546  1.1  christos {
    547  1.1  christos   membuf_object *membuf_obj = (membuf_object *) self;
    548  1.1  christos 
    549  1.1  christos   return PyString_FromFormat (_("Memory buffer for address %s, \
    550  1.1  christos which is %s bytes long."),
    551  1.1  christos 			      paddress (python_gdbarch, membuf_obj->addr),
    552  1.1  christos 			      pulongest (membuf_obj->length));
    553  1.1  christos }
    554  1.1  christos 
    555  1.1  christos #ifdef IS_PY3K
    556  1.1  christos 
    557  1.1  christos static int
    558  1.1  christos get_buffer (PyObject *self, Py_buffer *buf, int flags)
    559  1.1  christos {
    560  1.1  christos   membuf_object *membuf_obj = (membuf_object *) self;
    561  1.1  christos   int ret;
    562  1.1  christos 
    563  1.1  christos   ret = PyBuffer_FillInfo (buf, self, membuf_obj->buffer,
    564  1.1  christos 			   membuf_obj->length, 0,
    565  1.1  christos 			   PyBUF_CONTIG);
    566  1.1  christos   buf->format = "c";
    567  1.1  christos 
    568  1.1  christos   return ret;
    569  1.1  christos }
    570  1.1  christos 
    571  1.1  christos #else
    572  1.1  christos 
    573  1.1  christos static Py_ssize_t
    574  1.1  christos get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
    575  1.1  christos {
    576  1.1  christos   membuf_object *membuf_obj = (membuf_object *) self;
    577  1.1  christos 
    578  1.1  christos   if (segment)
    579  1.1  christos     {
    580  1.1  christos       PyErr_SetString (PyExc_SystemError,
    581  1.1  christos 		       _("The memory buffer supports only one segment."));
    582  1.1  christos       return -1;
    583  1.1  christos     }
    584  1.1  christos 
    585  1.1  christos   *ptrptr = membuf_obj->buffer;
    586  1.1  christos 
    587  1.1  christos   return membuf_obj->length;
    588  1.1  christos }
    589  1.1  christos 
    590  1.1  christos static Py_ssize_t
    591  1.1  christos get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
    592  1.1  christos {
    593  1.1  christos   return get_read_buffer (self, segment, ptrptr);
    594  1.1  christos }
    595  1.1  christos 
    596  1.1  christos static Py_ssize_t
    597  1.1  christos get_seg_count (PyObject *self, Py_ssize_t *lenp)
    598  1.1  christos {
    599  1.1  christos   if (lenp)
    600  1.1  christos     *lenp = ((membuf_object *) self)->length;
    601  1.1  christos 
    602  1.1  christos   return 1;
    603  1.1  christos }
    604  1.1  christos 
    605  1.1  christos static Py_ssize_t
    606  1.1  christos get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
    607  1.1  christos {
    608  1.1  christos   void *ptr = NULL;
    609  1.1  christos   Py_ssize_t ret;
    610  1.1  christos 
    611  1.1  christos   ret = get_read_buffer (self, segment, &ptr);
    612  1.1  christos   *ptrptr = (char *) ptr;
    613  1.1  christos 
    614  1.1  christos   return ret;
    615  1.1  christos }
    616  1.1  christos 
    617  1.1  christos #endif	/* IS_PY3K */
    618  1.1  christos 
    619  1.1  christos /* Implementation of
    620  1.1  christos    gdb.search_memory (address, length, pattern).  ADDRESS is the
    621  1.1  christos    address to start the search.  LENGTH specifies the scope of the
    622  1.1  christos    search from ADDRESS.  PATTERN is the pattern to search for (and
    623  1.1  christos    must be a Python object supporting the buffer protocol).
    624  1.1  christos    Returns a Python Long object holding the address where the pattern
    625  1.1  christos    was located, or if the pattern was not found, returns None.  Returns NULL
    626  1.1  christos    on error, with a python exception set.  */
    627  1.1  christos static PyObject *
    628  1.1  christos infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
    629  1.1  christos {
    630  1.1  christos   CORE_ADDR start_addr, length;
    631  1.1  christos   static char *keywords[] = { "address", "length", "pattern", NULL };
    632  1.1  christos   PyObject *start_addr_obj, *length_obj;
    633  1.1  christos   volatile struct gdb_exception except;
    634  1.1  christos   Py_ssize_t pattern_size;
    635  1.1  christos   const void *buffer;
    636  1.1  christos   CORE_ADDR found_addr;
    637  1.1  christos   int found = 0;
    638  1.1  christos #ifdef IS_PY3K
    639  1.1  christos   Py_buffer pybuf;
    640  1.1  christos 
    641  1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords,
    642  1.1  christos 				     &start_addr_obj, &length_obj,
    643  1.1  christos 				     &pybuf))
    644  1.1  christos     return NULL;
    645  1.1  christos 
    646  1.1  christos   buffer = pybuf.buf;
    647  1.1  christos   pattern_size = pybuf.len;
    648  1.1  christos #else
    649  1.1  christos   PyObject *pattern;
    650  1.1  christos 
    651  1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
    652  1.1  christos  				     &start_addr_obj, &length_obj,
    653  1.1  christos 				     &pattern))
    654  1.1  christos      return NULL;
    655  1.1  christos 
    656  1.1  christos   if (!PyObject_CheckReadBuffer (pattern))
    657  1.1  christos     {
    658  1.1  christos       PyErr_SetString (PyExc_RuntimeError,
    659  1.1  christos 		       _("The pattern is not a Python buffer."));
    660  1.1  christos 
    661  1.1  christos       return NULL;
    662  1.1  christos     }
    663  1.1  christos 
    664  1.1  christos   if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1)
    665  1.1  christos     return NULL;
    666  1.1  christos #endif
    667  1.1  christos 
    668  1.1  christos   if (get_addr_from_python (start_addr_obj, &start_addr) < 0)
    669  1.1  christos     goto fail;
    670  1.1  christos 
    671  1.1  christos   if (get_addr_from_python (length_obj, &length) < 0)
    672  1.1  christos     goto fail;
    673  1.1  christos 
    674  1.1  christos   if (!length)
    675  1.1  christos     {
    676  1.1  christos       PyErr_SetString (PyExc_ValueError,
    677  1.1  christos 		       _("Search range is empty."));
    678  1.1  christos       goto fail;
    679  1.1  christos     }
    680  1.1  christos   /* Watch for overflows.  */
    681  1.1  christos   else if (length > CORE_ADDR_MAX
    682  1.1  christos 	   || (start_addr + length - 1) < start_addr)
    683  1.1  christos     {
    684  1.1  christos       PyErr_SetString (PyExc_ValueError,
    685  1.1  christos 		       _("The search range is too large."));
    686  1.1  christos       goto fail;
    687  1.1  christos     }
    688  1.1  christos 
    689  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    690  1.1  christos     {
    691  1.1  christos       found = target_search_memory (start_addr, length,
    692  1.1  christos 				    buffer, pattern_size,
    693  1.1  christos 				    &found_addr);
    694  1.1  christos     }
    695  1.1  christos #ifdef IS_PY3K
    696  1.1  christos   PyBuffer_Release (&pybuf);
    697  1.1  christos #endif
    698  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    699  1.1  christos 
    700  1.1  christos   if (found)
    701  1.1  christos     return PyLong_FromLong (found_addr);
    702  1.1  christos   else
    703  1.1  christos     Py_RETURN_NONE;
    704  1.1  christos 
    705  1.1  christos  fail:
    706  1.1  christos #ifdef IS_PY3K
    707  1.1  christos   PyBuffer_Release (&pybuf);
    708  1.1  christos #endif
    709  1.1  christos   return NULL;
    710  1.1  christos }
    711  1.1  christos 
    712  1.1  christos /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
    713  1.1  christos    Returns True if this inferior object still exists in GDB.  */
    714  1.1  christos 
    715  1.1  christos static PyObject *
    716  1.1  christos infpy_is_valid (PyObject *self, PyObject *args)
    717  1.1  christos {
    718  1.1  christos   inferior_object *inf = (inferior_object *) self;
    719  1.1  christos 
    720  1.1  christos   if (! inf->inferior)
    721  1.1  christos     Py_RETURN_FALSE;
    722  1.1  christos 
    723  1.1  christos   Py_RETURN_TRUE;
    724  1.1  christos }
    725  1.1  christos 
    726  1.1  christos static void
    727  1.1  christos infpy_dealloc (PyObject *obj)
    728  1.1  christos {
    729  1.1  christos   inferior_object *inf_obj = (inferior_object *) obj;
    730  1.1  christos   struct inferior *inf = inf_obj->inferior;
    731  1.1  christos 
    732  1.1  christos   if (! inf)
    733  1.1  christos     return;
    734  1.1  christos 
    735  1.1  christos   set_inferior_data (inf, infpy_inf_data_key, NULL);
    736  1.1  christos }
    737  1.1  christos 
    738  1.1  christos /* Clear the INFERIOR pointer in an Inferior object and clear the
    739  1.1  christos    thread list.  */
    740  1.1  christos static void
    741  1.1  christos py_free_inferior (struct inferior *inf, void *datum)
    742  1.1  christos {
    743  1.1  christos 
    744  1.1  christos   struct cleanup *cleanup;
    745  1.1  christos   inferior_object *inf_obj = datum;
    746  1.1  christos   struct threadlist_entry *th_entry, *th_tmp;
    747  1.1  christos 
    748  1.1  christos   if (!gdb_python_initialized)
    749  1.1  christos     return;
    750  1.1  christos 
    751  1.1  christos   cleanup = ensure_python_env (python_gdbarch, python_language);
    752  1.1  christos 
    753  1.1  christos   inf_obj->inferior = NULL;
    754  1.1  christos 
    755  1.1  christos   /* Deallocate threads list.  */
    756  1.1  christos   for (th_entry = inf_obj->threads; th_entry != NULL;)
    757  1.1  christos     {
    758  1.1  christos       Py_DECREF (th_entry->thread_obj);
    759  1.1  christos 
    760  1.1  christos       th_tmp = th_entry;
    761  1.1  christos       th_entry = th_entry->next;
    762  1.1  christos       xfree (th_tmp);
    763  1.1  christos     }
    764  1.1  christos 
    765  1.1  christos   inf_obj->nthreads = 0;
    766  1.1  christos 
    767  1.1  christos   Py_DECREF ((PyObject *) inf_obj);
    768  1.1  christos   do_cleanups (cleanup);
    769  1.1  christos }
    770  1.1  christos 
    771  1.1  christos /* Implementation of gdb.selected_inferior() -> gdb.Inferior.
    772  1.1  christos    Returns the current inferior object.  */
    773  1.1  christos 
    774  1.1  christos PyObject *
    775  1.1  christos gdbpy_selected_inferior (PyObject *self, PyObject *args)
    776  1.1  christos {
    777  1.1  christos   return inferior_to_inferior_object (current_inferior ());
    778  1.1  christos }
    779  1.1  christos 
    780  1.1  christos int
    781  1.1  christos gdbpy_initialize_inferior (void)
    782  1.1  christos {
    783  1.1  christos   if (PyType_Ready (&inferior_object_type) < 0)
    784  1.1  christos     return -1;
    785  1.1  christos 
    786  1.1  christos   if (gdb_pymodule_addobject (gdb_module, "Inferior",
    787  1.1  christos 			      (PyObject *) &inferior_object_type) < 0)
    788  1.1  christos     return -1;
    789  1.1  christos 
    790  1.1  christos   infpy_inf_data_key =
    791  1.1  christos     register_inferior_data_with_cleanup (NULL, py_free_inferior);
    792  1.1  christos 
    793  1.1  christos   observer_attach_new_thread (add_thread_object);
    794  1.1  christos   observer_attach_thread_exit (delete_thread_object);
    795  1.1  christos   observer_attach_normal_stop (python_on_normal_stop);
    796  1.1  christos   observer_attach_target_resumed (python_on_resume);
    797  1.1  christos   observer_attach_inferior_exit (python_inferior_exit);
    798  1.1  christos   observer_attach_new_objfile (python_new_objfile);
    799  1.1  christos 
    800  1.1  christos   membuf_object_type.tp_new = PyType_GenericNew;
    801  1.1  christos   if (PyType_Ready (&membuf_object_type) < 0)
    802  1.1  christos     return -1;
    803  1.1  christos 
    804  1.1  christos   return gdb_pymodule_addobject (gdb_module, "Membuf", (PyObject *)
    805  1.1  christos 				 &membuf_object_type);
    806  1.1  christos }
    807  1.1  christos 
    808  1.1  christos static PyGetSetDef inferior_object_getset[] =
    809  1.1  christos {
    810  1.1  christos   { "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL },
    811  1.1  christos   { "pid", infpy_get_pid, NULL, "PID of inferior, as assigned by the OS.",
    812  1.1  christos     NULL },
    813  1.1  christos   { "was_attached", infpy_get_was_attached, NULL,
    814  1.1  christos     "True if the inferior was created using 'attach'.", NULL },
    815  1.1  christos   { NULL }
    816  1.1  christos };
    817  1.1  christos 
    818  1.1  christos static PyMethodDef inferior_object_methods[] =
    819  1.1  christos {
    820  1.1  christos   { "is_valid", infpy_is_valid, METH_NOARGS,
    821  1.1  christos     "is_valid () -> Boolean.\n\
    822  1.1  christos Return true if this inferior is valid, false if not." },
    823  1.1  christos   { "threads", infpy_threads, METH_NOARGS,
    824  1.1  christos     "Return all the threads of this inferior." },
    825  1.1  christos   { "read_memory", (PyCFunction) infpy_read_memory,
    826  1.1  christos     METH_VARARGS | METH_KEYWORDS,
    827  1.1  christos     "read_memory (address, length) -> buffer\n\
    828  1.1  christos Return a buffer object for reading from the inferior's memory." },
    829  1.1  christos   { "write_memory", (PyCFunction) infpy_write_memory,
    830  1.1  christos     METH_VARARGS | METH_KEYWORDS,
    831  1.1  christos     "write_memory (address, buffer [, length])\n\
    832  1.1  christos Write the given buffer object to the inferior's memory." },
    833  1.1  christos   { "search_memory", (PyCFunction) infpy_search_memory,
    834  1.1  christos     METH_VARARGS | METH_KEYWORDS,
    835  1.1  christos     "search_memory (address, length, pattern) -> long\n\
    836  1.1  christos Return a long with the address of a match, or None." },
    837  1.1  christos   { NULL }
    838  1.1  christos };
    839  1.1  christos 
    840  1.1  christos static PyTypeObject inferior_object_type =
    841  1.1  christos {
    842  1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    843  1.1  christos   "gdb.Inferior",		  /* tp_name */
    844  1.1  christos   sizeof (inferior_object),	  /* tp_basicsize */
    845  1.1  christos   0,				  /* tp_itemsize */
    846  1.1  christos   infpy_dealloc,		  /* tp_dealloc */
    847  1.1  christos   0,				  /* tp_print */
    848  1.1  christos   0,				  /* tp_getattr */
    849  1.1  christos   0,				  /* tp_setattr */
    850  1.1  christos   0,				  /* tp_compare */
    851  1.1  christos   0,				  /* tp_repr */
    852  1.1  christos   0,				  /* tp_as_number */
    853  1.1  christos   0,				  /* tp_as_sequence */
    854  1.1  christos   0,				  /* tp_as_mapping */
    855  1.1  christos   0,				  /* tp_hash  */
    856  1.1  christos   0,				  /* tp_call */
    857  1.1  christos   0,				  /* tp_str */
    858  1.1  christos   0,				  /* tp_getattro */
    859  1.1  christos   0,				  /* tp_setattro */
    860  1.1  christos   0,				  /* tp_as_buffer */
    861  1.1  christos   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,  /* tp_flags */
    862  1.1  christos   "GDB inferior object",	  /* tp_doc */
    863  1.1  christos   0,				  /* tp_traverse */
    864  1.1  christos   0,				  /* tp_clear */
    865  1.1  christos   0,				  /* tp_richcompare */
    866  1.1  christos   0,				  /* tp_weaklistoffset */
    867  1.1  christos   0,				  /* tp_iter */
    868  1.1  christos   0,				  /* tp_iternext */
    869  1.1  christos   inferior_object_methods,	  /* tp_methods */
    870  1.1  christos   0,				  /* tp_members */
    871  1.1  christos   inferior_object_getset,	  /* tp_getset */
    872  1.1  christos   0,				  /* tp_base */
    873  1.1  christos   0,				  /* tp_dict */
    874  1.1  christos   0,				  /* tp_descr_get */
    875  1.1  christos   0,				  /* tp_descr_set */
    876  1.1  christos   0,				  /* tp_dictoffset */
    877  1.1  christos   0,				  /* tp_init */
    878  1.1  christos   0				  /* tp_alloc */
    879  1.1  christos };
    880  1.1  christos 
    881  1.1  christos #ifdef IS_PY3K
    882  1.1  christos 
    883  1.1  christos static PyBufferProcs buffer_procs =
    884  1.1  christos {
    885  1.1  christos   get_buffer
    886  1.1  christos };
    887  1.1  christos 
    888  1.1  christos #else
    889  1.1  christos 
    890  1.1  christos /* Python doesn't provide a decent way to get compatibility here.  */
    891  1.1  christos #if HAVE_LIBPYTHON2_4
    892  1.1  christos #define CHARBUFFERPROC_NAME getcharbufferproc
    893  1.1  christos #else
    894  1.1  christos #define CHARBUFFERPROC_NAME charbufferproc
    895  1.1  christos #endif
    896  1.1  christos 
    897  1.1  christos static PyBufferProcs buffer_procs = {
    898  1.1  christos   get_read_buffer,
    899  1.1  christos   get_write_buffer,
    900  1.1  christos   get_seg_count,
    901  1.1  christos   /* The cast here works around a difference between Python 2.4 and
    902  1.1  christos      Python 2.5.  */
    903  1.1  christos   (CHARBUFFERPROC_NAME) get_char_buffer
    904  1.1  christos };
    905  1.1  christos #endif	/* IS_PY3K */
    906  1.1  christos 
    907  1.1  christos static PyTypeObject membuf_object_type = {
    908  1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    909  1.1  christos   "gdb.Membuf",			  /*tp_name*/
    910  1.1  christos   sizeof (membuf_object),	  /*tp_basicsize*/
    911  1.1  christos   0,				  /*tp_itemsize*/
    912  1.1  christos   mbpy_dealloc,			  /*tp_dealloc*/
    913  1.1  christos   0,				  /*tp_print*/
    914  1.1  christos   0,				  /*tp_getattr*/
    915  1.1  christos   0,				  /*tp_setattr*/
    916  1.1  christos   0,				  /*tp_compare*/
    917  1.1  christos   0,				  /*tp_repr*/
    918  1.1  christos   0,				  /*tp_as_number*/
    919  1.1  christos   0,				  /*tp_as_sequence*/
    920  1.1  christos   0,				  /*tp_as_mapping*/
    921  1.1  christos   0,				  /*tp_hash */
    922  1.1  christos   0,				  /*tp_call*/
    923  1.1  christos   mbpy_str,			  /*tp_str*/
    924  1.1  christos   0,				  /*tp_getattro*/
    925  1.1  christos   0,				  /*tp_setattro*/
    926  1.1  christos   &buffer_procs,		  /*tp_as_buffer*/
    927  1.1  christos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
    928  1.1  christos   "GDB memory buffer object", 	  /*tp_doc*/
    929  1.1  christos   0,				  /* tp_traverse */
    930  1.1  christos   0,				  /* tp_clear */
    931  1.1  christos   0,				  /* tp_richcompare */
    932  1.1  christos   0,				  /* tp_weaklistoffset */
    933  1.1  christos   0,				  /* tp_iter */
    934  1.1  christos   0,				  /* tp_iternext */
    935  1.1  christos   0,				  /* tp_methods */
    936  1.1  christos   0,				  /* tp_members */
    937  1.1  christos   0,				  /* tp_getset */
    938  1.1  christos   0,				  /* tp_base */
    939  1.1  christos   0,				  /* tp_dict */
    940  1.1  christos   0,				  /* tp_descr_get */
    941  1.1  christos   0,				  /* tp_descr_set */
    942  1.1  christos   0,				  /* tp_dictoffset */
    943  1.1  christos   0,				  /* tp_init */
    944  1.1  christos   0,				  /* tp_alloc */
    945  1.1  christos };
    946