* python/py-inferior.c (infpy_read_memory): Remove cleanups and
[platform/upstream/binutils.git] / gdb / python / py-inferior.c
1 /* Python interface to inferiors.
2
3    Copyright (C) 2009-2012 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 target_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 inflist_entry *p;
184   struct inferior *inf = find_inferior_pid (pid);
185
186   if (inf)
187     return inferior_to_inferior_object (inf);
188
189   return NULL;
190 }
191
192 thread_object *
193 find_thread_object (ptid_t ptid)
194 {
195   int pid;
196   struct threadlist_entry *thread;
197   PyObject *inf_obj;
198   thread_object *found = NULL;
199
200   pid = PIDGET (ptid);
201   if (pid == 0)
202     return NULL;
203
204   inf_obj = find_inferior_object (pid);
205
206   if (! inf_obj)
207     return NULL;
208
209   for (thread = ((inferior_object *)inf_obj)->threads; thread;
210        thread = thread->next)
211     if (ptid_equal (thread->thread_obj->thread->ptid, ptid))
212       {
213         found = thread->thread_obj;
214         break;
215       }
216
217   Py_DECREF (inf_obj);
218
219   if (found)
220     return found;
221
222   return NULL;
223 }
224
225 static void
226 add_thread_object (struct thread_info *tp)
227 {
228   struct cleanup *cleanup;
229   thread_object *thread_obj;
230   inferior_object *inf_obj;
231   struct threadlist_entry *entry;
232
233   cleanup = ensure_python_env (python_gdbarch, python_language);
234
235   thread_obj = create_thread_object (tp);
236   if (!thread_obj)
237     {
238       gdbpy_print_stack ();
239       do_cleanups (cleanup);
240       return;
241     }
242
243   inf_obj = (inferior_object *) thread_obj->inf_obj;
244
245   entry = xmalloc (sizeof (struct threadlist_entry));
246   entry->thread_obj = thread_obj;
247   entry->next = inf_obj->threads;
248
249   inf_obj->threads = entry;
250   inf_obj->nthreads++;
251
252   do_cleanups (cleanup);
253 }
254
255 static void
256 delete_thread_object (struct thread_info *tp, int ignore)
257 {
258   struct cleanup *cleanup;
259   inferior_object *inf_obj;
260   thread_object *thread_obj;
261   struct threadlist_entry **entry, *tmp;
262   
263   cleanup = ensure_python_env (python_gdbarch, python_language);
264
265   inf_obj = (inferior_object *) find_inferior_object (PIDGET(tp->ptid));
266   if (!inf_obj)
267     {
268       do_cleanups (cleanup);
269       return;
270     }
271
272   /* Find thread entry in its inferior's thread_list.  */
273   for (entry = &inf_obj->threads; *entry != NULL; entry =
274          &(*entry)->next)
275     if ((*entry)->thread_obj->thread == tp)
276       break;
277
278   if (!*entry)
279     {
280       Py_DECREF (inf_obj);
281       do_cleanups (cleanup);
282       return;
283     }
284
285   tmp = *entry;
286   tmp->thread_obj->thread = NULL;
287
288   *entry = (*entry)->next;
289   inf_obj->nthreads--;
290
291   Py_DECREF (tmp->thread_obj);
292   Py_DECREF (inf_obj);
293   xfree (tmp);
294
295   do_cleanups (cleanup);
296 }
297
298 static PyObject *
299 infpy_threads (PyObject *self, PyObject *args)
300 {
301   int i;
302   struct threadlist_entry *entry;
303   inferior_object *inf_obj = (inferior_object *) self;
304   PyObject *tuple;
305
306   INFPY_REQUIRE_VALID (inf_obj);
307
308   tuple = PyTuple_New (inf_obj->nthreads);
309   if (!tuple)
310     return NULL;
311
312   for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads;
313        i++, entry = entry->next)
314     {
315       Py_INCREF (entry->thread_obj);
316       PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj);
317     }
318
319   return tuple;
320 }
321
322 static PyObject *
323 infpy_get_num (PyObject *self, void *closure)
324 {
325   inferior_object *inf = (inferior_object *) self;
326
327   INFPY_REQUIRE_VALID (inf);
328
329   return PyLong_FromLong (inf->inferior->num);
330 }
331
332 static PyObject *
333 infpy_get_pid (PyObject *self, void *closure)
334 {
335   inferior_object *inf = (inferior_object *) self;
336
337   INFPY_REQUIRE_VALID (inf);
338
339   return PyLong_FromLong (inf->inferior->pid);
340 }
341
342 static PyObject *
343 infpy_get_was_attached (PyObject *self, void *closure)
344 {
345   inferior_object *inf = (inferior_object *) self;
346
347   INFPY_REQUIRE_VALID (inf);
348   if (inf->inferior->attach_flag)
349     Py_RETURN_TRUE;
350   Py_RETURN_FALSE;
351 }
352
353 static int
354 build_inferior_list (struct inferior *inf, void *arg)
355 {
356   PyObject *list = arg;
357   PyObject *inferior = inferior_to_inferior_object (inf);
358   int success = 0;
359
360   if (! inferior)
361     return 0;
362
363   success = PyList_Append (list, inferior);
364   Py_DECREF (inferior);
365
366   if (success)
367     return 1;
368
369   return 0;
370 }
371
372 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
373    Returns a tuple of all inferiors.  */
374 PyObject *
375 gdbpy_inferiors (PyObject *unused, PyObject *unused2)
376 {
377   PyObject *list, *tuple;
378
379   list = PyList_New (0);
380   if (!list)
381     return NULL;
382
383   if (iterate_over_inferiors (build_inferior_list, list))
384     {
385       Py_DECREF (list);
386       return NULL;
387     }
388
389   tuple = PyList_AsTuple (list);
390   Py_DECREF (list);
391
392   return tuple;
393 }
394
395 /* Membuf and memory manipulation.  */
396
397 /* Implementation of gdb.read_memory (address, length).
398    Returns a Python buffer object with LENGTH bytes of the inferior's
399    memory at ADDRESS.  Both arguments are integers.  Returns NULL on error,
400    with a python exception set.  */
401 static PyObject *
402 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
403 {
404   int error = 0;
405   CORE_ADDR addr, length;
406   void *buffer = NULL;
407   membuf_object *membuf_obj;
408   PyObject *addr_obj, *length_obj, *result;
409   volatile struct gdb_exception except;
410   static char *keywords[] = { "address", "length", NULL };
411
412   if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
413                                      &addr_obj, &length_obj))
414     return NULL;
415
416   TRY_CATCH (except, RETURN_MASK_ALL)
417     {
418       if (!get_addr_from_python (addr_obj, &addr)
419           || !get_addr_from_python (length_obj, &length))
420         {
421           error = 1;
422           break;
423         }
424
425       buffer = xmalloc (length);
426
427       read_memory (addr, buffer, length);
428     }
429   if (except.reason < 0)
430     {
431       xfree (buffer);
432       GDB_PY_HANDLE_EXCEPTION (except);
433     }
434
435   if (error)
436     {
437       xfree (buffer);
438       return NULL;
439     }
440
441   membuf_obj = PyObject_New (membuf_object, &membuf_object_type);
442   if (membuf_obj == NULL)
443     {
444       xfree (buffer);
445       PyErr_SetString (PyExc_MemoryError,
446                        _("Could not allocate memory buffer object."));
447       return NULL;
448     }
449
450   membuf_obj->buffer = buffer;
451   membuf_obj->addr = addr;
452   membuf_obj->length = length;
453
454   result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
455                                          Py_END_OF_BUFFER);
456   Py_DECREF (membuf_obj);
457   return result;
458 }
459
460 /* Implementation of gdb.write_memory (address, buffer [, length]).
461    Writes the contents of BUFFER (a Python object supporting the read
462    buffer protocol) at ADDRESS in the inferior's memory.  Write LENGTH
463    bytes from BUFFER, or its entire contents if the argument is not
464    provided.  The function returns nothing.  Returns NULL on error, with
465    a python exception set.  */
466 static PyObject *
467 infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
468 {
469   Py_ssize_t buf_len;
470   int error = 0;
471   const char *buffer;
472   CORE_ADDR addr, length;
473   PyObject *addr_obj, *length_obj = NULL;
474   volatile struct gdb_exception except;
475   static char *keywords[] = { "address", "buffer", "length", NULL };
476
477
478   if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
479                                      &addr_obj, &buffer, &buf_len,
480                                      &length_obj))
481     return NULL;
482
483   TRY_CATCH (except, RETURN_MASK_ALL)
484     {
485       if (!get_addr_from_python (addr_obj, &addr))
486         {
487           error = 1;
488           break;
489         }
490
491       if (!length_obj)
492         length = buf_len;
493       else if (!get_addr_from_python (length_obj, &length))
494         {
495           error = 1;
496           break;
497         }
498       write_memory (addr, buffer, length);
499     }
500   GDB_PY_HANDLE_EXCEPTION (except);
501
502   if (error)
503     return NULL;
504
505   Py_RETURN_NONE;
506 }
507
508 /* Destructor of Membuf objects.  */
509 static void
510 mbpy_dealloc (PyObject *self)
511 {
512   xfree (((membuf_object *) self)->buffer);
513   self->ob_type->tp_free (self);
514 }
515
516 /* Return a description of the Membuf object.  */
517 static PyObject *
518 mbpy_str (PyObject *self)
519 {
520   membuf_object *membuf_obj = (membuf_object *) self;
521
522   return PyString_FromFormat (_("Memory buffer for address %s, \
523 which is %s bytes long."),
524                               paddress (python_gdbarch, membuf_obj->addr),
525                               pulongest (membuf_obj->length));
526 }
527
528 static Py_ssize_t
529 get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
530 {
531   membuf_object *membuf_obj = (membuf_object *) self;
532
533   if (segment)
534     {
535       PyErr_SetString (PyExc_SystemError,
536                        _("The memory buffer supports only one segment."));
537       return -1;
538     }
539
540   *ptrptr = membuf_obj->buffer;
541
542   return membuf_obj->length;
543 }
544
545 static Py_ssize_t
546 get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
547 {
548   return get_read_buffer (self, segment, ptrptr);
549 }
550
551 static Py_ssize_t
552 get_seg_count (PyObject *self, Py_ssize_t *lenp)
553 {
554   if (lenp)
555     *lenp = ((membuf_object *) self)->length;
556
557   return 1;
558 }
559
560 static Py_ssize_t
561 get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
562 {
563   void *ptr = NULL;
564   Py_ssize_t ret;
565
566   ret = get_read_buffer (self, segment, &ptr);
567   *ptrptr = (char *) ptr;
568
569   return ret;
570 }
571
572 /* Implementation of
573    gdb.search_memory (address, length, pattern).  ADDRESS is the
574    address to start the search.  LENGTH specifies the scope of the
575    search from ADDRESS.  PATTERN is the pattern to search for (and
576    must be a Python object supporting the buffer protocol).
577    Returns a Python Long object holding the address where the pattern
578    was located, or if the pattern was not found, returns None.  Returns NULL
579    on error, with a python exception set.  */
580 static PyObject *
581 infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
582 {
583   CORE_ADDR start_addr, length;
584   static char *keywords[] = { "address", "length", "pattern", NULL };
585   PyObject *pattern, *start_addr_obj, *length_obj;
586   volatile struct gdb_exception except;
587   Py_ssize_t pattern_size;
588   const void *buffer;
589   CORE_ADDR found_addr;
590   int found = 0;
591
592   if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
593                                      &start_addr_obj, &length_obj,
594                                      &pattern))
595     return NULL;
596
597   if (get_addr_from_python (start_addr_obj, &start_addr)
598       && get_addr_from_python (length_obj, &length))
599     {
600       if (!length)
601         {
602           PyErr_SetString (PyExc_ValueError,
603                            _("Search range is empty."));
604           return NULL;
605         }
606       /* Watch for overflows.  */
607       else if (length > CORE_ADDR_MAX
608                || (start_addr + length - 1) < start_addr)
609         {
610           PyErr_SetString (PyExc_ValueError,
611                            _("The search range is too large."));
612
613           return NULL;
614         }
615     }
616   else
617     return NULL;
618
619   if (!PyObject_CheckReadBuffer (pattern))
620     {
621       PyErr_SetString (PyExc_RuntimeError,
622                        _("The pattern is not a Python buffer."));
623
624       return NULL;
625     }
626
627   if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1)
628     return NULL;
629
630   TRY_CATCH (except, RETURN_MASK_ALL)
631     {
632       found = target_search_memory (start_addr, length,
633                                     buffer, pattern_size,
634                                     &found_addr);
635     }
636   GDB_PY_HANDLE_EXCEPTION (except);
637
638   if (found)
639     return PyLong_FromLong (found_addr);
640   else
641     Py_RETURN_NONE;
642 }
643
644 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
645    Returns True if this inferior object still exists in GDB.  */
646
647 static PyObject *
648 infpy_is_valid (PyObject *self, PyObject *args)
649 {
650   inferior_object *inf = (inferior_object *) self;
651
652   if (! inf->inferior)
653     Py_RETURN_FALSE;
654
655   Py_RETURN_TRUE;
656 }
657
658 static void
659 infpy_dealloc (PyObject *obj)
660 {
661   inferior_object *inf_obj = (inferior_object *) obj;
662   struct inferior *inf = inf_obj->inferior;
663
664   if (! inf)
665     return;
666
667   set_inferior_data (inf, infpy_inf_data_key, NULL);
668 }
669
670 /* Clear the INFERIOR pointer in an Inferior object and clear the
671    thread list.  */
672 static void
673 py_free_inferior (struct inferior *inf, void *datum)
674 {
675
676   struct cleanup *cleanup;
677   inferior_object *inf_obj = datum;
678   struct threadlist_entry *th_entry, *th_tmp;
679
680   cleanup = ensure_python_env (python_gdbarch, python_language);
681
682   inf_obj->inferior = NULL;
683
684   /* Deallocate threads list.  */
685   for (th_entry = inf_obj->threads; th_entry != NULL;)
686     {
687       Py_DECREF (th_entry->thread_obj);
688
689       th_tmp = th_entry;
690       th_entry = th_entry->next;
691       xfree (th_tmp);
692     }
693
694   inf_obj->nthreads = 0;
695
696   Py_DECREF ((PyObject *) inf_obj);
697   do_cleanups (cleanup);
698 }
699
700 /* Implementation of gdb.selected_inferior() -> gdb.Inferior.
701    Returns the current inferior object.  */
702
703 PyObject *
704 gdbpy_selected_inferior (PyObject *self, PyObject *args)
705 {
706   PyObject *inf_obj;
707
708   inf_obj = inferior_to_inferior_object (current_inferior ());
709   Py_INCREF (inf_obj);
710
711   return inf_obj;
712 }
713
714 void
715 gdbpy_initialize_inferior (void)
716 {
717   if (PyType_Ready (&inferior_object_type) < 0)
718     return;
719
720   Py_INCREF (&inferior_object_type);
721   PyModule_AddObject (gdb_module, "Inferior",
722                       (PyObject *) &inferior_object_type);
723
724   infpy_inf_data_key =
725     register_inferior_data_with_cleanup (py_free_inferior);
726
727   observer_attach_new_thread (add_thread_object);
728   observer_attach_thread_exit (delete_thread_object);
729   observer_attach_normal_stop (python_on_normal_stop);
730   observer_attach_target_resumed (python_on_resume);
731   observer_attach_inferior_exit (python_inferior_exit);
732   observer_attach_new_objfile (python_new_objfile);
733
734   membuf_object_type.tp_new = PyType_GenericNew;
735   if (PyType_Ready (&membuf_object_type) < 0)
736     return;
737
738   Py_INCREF (&membuf_object_type);
739   PyModule_AddObject (gdb_module, "Membuf", (PyObject *)
740                       &membuf_object_type);
741 }
742
743 static PyGetSetDef inferior_object_getset[] =
744 {
745   { "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL },
746   { "pid", infpy_get_pid, NULL, "PID of inferior, as assigned by the OS.",
747     NULL },
748   { "was_attached", infpy_get_was_attached, NULL,
749     "True if the inferior was created using 'attach'.", NULL },
750   { NULL }
751 };
752
753 static PyMethodDef inferior_object_methods[] =
754 {
755   { "is_valid", infpy_is_valid, METH_NOARGS,
756     "is_valid () -> Boolean.\n\
757 Return true if this inferior is valid, false if not." },
758   { "threads", infpy_threads, METH_NOARGS,
759     "Return all the threads of this inferior." },
760   { "read_memory", (PyCFunction) infpy_read_memory,
761     METH_VARARGS | METH_KEYWORDS,
762     "read_memory (address, length) -> buffer\n\
763 Return a buffer object for reading from the inferior's memory." },
764   { "write_memory", (PyCFunction) infpy_write_memory,
765     METH_VARARGS | METH_KEYWORDS,
766     "write_memory (address, buffer [, length])\n\
767 Write the given buffer object to the inferior's memory." },
768   { "search_memory", (PyCFunction) infpy_search_memory,
769     METH_VARARGS | METH_KEYWORDS,
770     "search_memory (address, length, pattern) -> long\n\
771 Return a long with the address of a match, or None." },
772   { NULL }
773 };
774
775 static PyTypeObject inferior_object_type =
776 {
777   PyObject_HEAD_INIT (NULL)
778   0,                              /* ob_size */
779   "gdb.Inferior",                 /* tp_name */
780   sizeof (inferior_object),       /* tp_basicsize */
781   0,                              /* tp_itemsize */
782   infpy_dealloc,                  /* tp_dealloc */
783   0,                              /* tp_print */
784   0,                              /* tp_getattr */
785   0,                              /* tp_setattr */
786   0,                              /* tp_compare */
787   0,                              /* tp_repr */
788   0,                              /* tp_as_number */
789   0,                              /* tp_as_sequence */
790   0,                              /* tp_as_mapping */
791   0,                              /* tp_hash  */
792   0,                              /* tp_call */
793   0,                              /* tp_str */
794   0,                              /* tp_getattro */
795   0,                              /* tp_setattro */
796   0,                              /* tp_as_buffer */
797   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,  /* tp_flags */
798   "GDB inferior object",          /* tp_doc */
799   0,                              /* tp_traverse */
800   0,                              /* tp_clear */
801   0,                              /* tp_richcompare */
802   0,                              /* tp_weaklistoffset */
803   0,                              /* tp_iter */
804   0,                              /* tp_iternext */
805   inferior_object_methods,        /* tp_methods */
806   0,                              /* tp_members */
807   inferior_object_getset,         /* tp_getset */
808   0,                              /* tp_base */
809   0,                              /* tp_dict */
810   0,                              /* tp_descr_get */
811   0,                              /* tp_descr_set */
812   0,                              /* tp_dictoffset */
813   0,                              /* tp_init */
814   0                               /* tp_alloc */
815 };
816
817 /* Python doesn't provide a decent way to get compatibility here.  */
818 #if HAVE_LIBPYTHON2_4
819 #define CHARBUFFERPROC_NAME getcharbufferproc
820 #else
821 #define CHARBUFFERPROC_NAME charbufferproc
822 #endif
823
824 static PyBufferProcs buffer_procs = {
825   get_read_buffer,
826   get_write_buffer,
827   get_seg_count,
828   /* The cast here works around a difference between Python 2.4 and
829      Python 2.5.  */
830   (CHARBUFFERPROC_NAME) get_char_buffer
831 };
832
833 static PyTypeObject membuf_object_type = {
834   PyObject_HEAD_INIT (NULL)
835   0,                              /*ob_size*/
836   "gdb.Membuf",                   /*tp_name*/
837   sizeof (membuf_object),         /*tp_basicsize*/
838   0,                              /*tp_itemsize*/
839   mbpy_dealloc,                   /*tp_dealloc*/
840   0,                              /*tp_print*/
841   0,                              /*tp_getattr*/
842   0,                              /*tp_setattr*/
843   0,                              /*tp_compare*/
844   0,                              /*tp_repr*/
845   0,                              /*tp_as_number*/
846   0,                              /*tp_as_sequence*/
847   0,                              /*tp_as_mapping*/
848   0,                              /*tp_hash */
849   0,                              /*tp_call*/
850   mbpy_str,                       /*tp_str*/
851   0,                              /*tp_getattro*/
852   0,                              /*tp_setattro*/
853   &buffer_procs,                  /*tp_as_buffer*/
854   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
855   "GDB memory buffer object",     /*tp_doc*/
856   0,                              /* tp_traverse */
857   0,                              /* tp_clear */
858   0,                              /* tp_richcompare */
859   0,                              /* tp_weaklistoffset */
860   0,                              /* tp_iter */
861   0,                              /* tp_iternext */
862   0,                              /* tp_methods */
863   0,                              /* tp_members */
864   0,                              /* tp_getset */
865   0,                              /* tp_base */
866   0,                              /* tp_dict */
867   0,                              /* tp_descr_get */
868   0,                              /* tp_descr_set */
869   0,                              /* tp_dictoffset */
870   0,                              /* tp_init */
871   0,                              /* tp_alloc */
872 };