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