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