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