1 /* Python interface to inferiors.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "auto-load.h"
23 #include "gdbthread.h"
26 #include "observable.h"
27 #include "python-internal.h"
28 #include "arch-utils.h"
30 #include "gdbsupport/gdb_signals.h"
32 #include "py-stopevent.h"
34 struct threadlist_entry
36 threadlist_entry (gdbpy_ref
<thread_object
> &&ref
)
37 : thread_obj (std::move (ref
))
41 gdbpy_ref
<thread_object
> thread_obj
;
42 struct threadlist_entry
*next
;
45 struct inferior_object
49 /* The inferior we represent. */
50 struct inferior
*inferior
;
52 /* thread_object instances under this inferior. This list owns a
53 reference to each object it contains. */
54 struct threadlist_entry
*threads
;
56 /* Number of threads in the list. */
60 extern PyTypeObject inferior_object_type
61 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object");
63 static const struct inferior_data
*infpy_inf_data_key
;
65 struct membuf_object
{
69 /* These are kept just for mbpy_str. */
74 extern PyTypeObject membuf_object_type
75 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("membuf_object");
77 /* Require that INFERIOR be a valid inferior ID. */
78 #define INFPY_REQUIRE_VALID(Inferior) \
80 if (!Inferior->inferior) \
82 PyErr_SetString (PyExc_RuntimeError, \
83 _("Inferior no longer exists.")); \
89 python_on_normal_stop (struct bpstats
*bs
, int print_frame
)
91 enum gdb_signal stop_signal
;
93 if (!gdb_python_initialized
)
96 if (inferior_ptid
== null_ptid
)
99 stop_signal
= inferior_thread ()->suspend
.stop_signal
;
101 gdbpy_enter
enter_py (get_current_arch (), current_language
);
103 if (emit_stop_event (bs
, stop_signal
) < 0)
104 gdbpy_print_stack ();
108 python_on_resume (ptid_t ptid
)
110 if (!gdb_python_initialized
)
113 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
115 if (emit_continue_event (ptid
) < 0)
116 gdbpy_print_stack ();
119 /* Callback, registered as an observer, that notifies Python listeners
120 when an inferior function call is about to be made. */
123 python_on_inferior_call_pre (ptid_t thread
, CORE_ADDR address
)
125 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
127 if (emit_inferior_call_event (INFERIOR_CALL_PRE
, thread
, address
) < 0)
128 gdbpy_print_stack ();
131 /* Callback, registered as an observer, that notifies Python listeners
132 when an inferior function call has completed. */
135 python_on_inferior_call_post (ptid_t thread
, CORE_ADDR address
)
137 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
139 if (emit_inferior_call_event (INFERIOR_CALL_POST
, thread
, address
) < 0)
140 gdbpy_print_stack ();
143 /* Callback, registered as an observer, that notifies Python listeners
144 when a part of memory has been modified by user action (eg via a
148 python_on_memory_change (struct inferior
*inferior
, CORE_ADDR addr
, ssize_t len
, const bfd_byte
*data
)
150 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
152 if (emit_memory_changed_event (addr
, len
) < 0)
153 gdbpy_print_stack ();
156 /* Callback, registered as an observer, that notifies Python listeners
157 when a register has been modified by user action (eg via a 'set'
161 python_on_register_change (struct frame_info
*frame
, int regnum
)
163 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
165 if (emit_register_changed_event (frame
, regnum
) < 0)
166 gdbpy_print_stack ();
170 python_inferior_exit (struct inferior
*inf
)
172 const LONGEST
*exit_code
= NULL
;
174 if (!gdb_python_initialized
)
177 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
179 if (inf
->has_exit_code
)
180 exit_code
= &inf
->exit_code
;
182 if (emit_exited_event (exit_code
, inf
) < 0)
183 gdbpy_print_stack ();
186 /* Callback used to notify Python listeners about new objfiles loaded in the
187 inferior. OBJFILE may be NULL which means that the objfile list has been
188 cleared (emptied). */
191 python_new_objfile (struct objfile
*objfile
)
193 if (!gdb_python_initialized
)
196 gdbpy_enter
enter_py (objfile
!= NULL
203 if (emit_clear_objfiles_event () < 0)
204 gdbpy_print_stack ();
208 if (emit_new_objfile_event (objfile
) < 0)
209 gdbpy_print_stack ();
213 /* Return a reference to the Python object of type Inferior
214 representing INFERIOR. If the object has already been created,
215 return it and increment the reference count, otherwise, create it.
216 Return NULL on failure. */
218 gdbpy_ref
<inferior_object
>
219 inferior_to_inferior_object (struct inferior
*inferior
)
221 inferior_object
*inf_obj
;
223 inf_obj
= (inferior_object
*) inferior_data (inferior
, infpy_inf_data_key
);
226 inf_obj
= PyObject_New (inferior_object
, &inferior_object_type
);
230 inf_obj
->inferior
= inferior
;
231 inf_obj
->threads
= NULL
;
232 inf_obj
->nthreads
= 0;
234 /* PyObject_New initializes the new object with a refcount of 1. This
235 counts for the reference we are keeping in the inferior data. */
236 set_inferior_data (inferior
, infpy_inf_data_key
, inf_obj
);
239 /* We are returning a new reference. */
240 gdb_assert (inf_obj
!= nullptr);
241 return gdbpy_ref
<inferior_object
>::new_reference (inf_obj
);
244 /* Called when a new inferior is created. Notifies any Python event
247 python_new_inferior (struct inferior
*inf
)
249 if (!gdb_python_initialized
)
252 gdbpy_enter
enter_py (python_gdbarch
, python_language
);
254 if (evregpy_no_listeners_p (gdb_py_events
.new_inferior
))
257 gdbpy_ref
<inferior_object
> inf_obj
= inferior_to_inferior_object (inf
);
260 gdbpy_print_stack ();
264 gdbpy_ref
<> event
= create_event_object (&new_inferior_event_object_type
);
266 || evpy_add_attribute (event
.get (), "inferior",
267 (PyObject
*) inf_obj
.get ()) < 0
268 || evpy_emit_event (event
.get (), gdb_py_events
.new_inferior
) < 0)
269 gdbpy_print_stack ();
272 /* Called when an inferior is removed. Notifies any Python event
275 python_inferior_deleted (struct inferior
*inf
)
277 if (!gdb_python_initialized
)
280 gdbpy_enter
enter_py (python_gdbarch
, python_language
);
282 if (evregpy_no_listeners_p (gdb_py_events
.inferior_deleted
))
285 gdbpy_ref
<inferior_object
> inf_obj
= inferior_to_inferior_object (inf
);
288 gdbpy_print_stack ();
292 gdbpy_ref
<> event
= create_event_object (&inferior_deleted_event_object_type
);
294 || evpy_add_attribute (event
.get (), "inferior",
295 (PyObject
*) inf_obj
.get ()) < 0
296 || evpy_emit_event (event
.get (), gdb_py_events
.inferior_deleted
) < 0)
297 gdbpy_print_stack ();
301 thread_to_thread_object (thread_info
*thr
)
303 gdbpy_ref
<inferior_object
> inf_obj
= inferior_to_inferior_object (thr
->inf
);
307 for (threadlist_entry
*thread
= inf_obj
->threads
;
309 thread
= thread
->next
)
310 if (thread
->thread_obj
->thread
== thr
)
311 return gdbpy_ref
<>::new_reference ((PyObject
*) thread
->thread_obj
.get ());
313 PyErr_SetString (PyExc_SystemError
,
314 _("could not find gdb thread object"));
319 add_thread_object (struct thread_info
*tp
)
321 inferior_object
*inf_obj
;
322 struct threadlist_entry
*entry
;
324 if (!gdb_python_initialized
)
327 gdbpy_enter
enter_py (python_gdbarch
, python_language
);
329 gdbpy_ref
<thread_object
> thread_obj
= create_thread_object (tp
);
330 if (thread_obj
== NULL
)
332 gdbpy_print_stack ();
336 inf_obj
= (inferior_object
*) thread_obj
->inf_obj
;
338 entry
= new threadlist_entry (std::move (thread_obj
));
339 entry
->next
= inf_obj
->threads
;
341 inf_obj
->threads
= entry
;
344 if (evregpy_no_listeners_p (gdb_py_events
.new_thread
))
347 gdbpy_ref
<> event
= create_thread_event_object (&new_thread_event_object_type
,
348 (PyObject
*) inf_obj
);
350 || evpy_emit_event (event
.get (), gdb_py_events
.new_thread
) < 0)
351 gdbpy_print_stack ();
355 delete_thread_object (struct thread_info
*tp
, int ignore
)
357 struct threadlist_entry
**entry
, *tmp
;
359 if (!gdb_python_initialized
)
362 gdbpy_enter
enter_py (python_gdbarch
, python_language
);
364 gdbpy_ref
<inferior_object
> inf_obj
= inferior_to_inferior_object (tp
->inf
);
368 /* Find thread entry in its inferior's thread_list. */
369 for (entry
= &inf_obj
->threads
; *entry
!= NULL
; entry
=
371 if ((*entry
)->thread_obj
->thread
== tp
)
378 tmp
->thread_obj
->thread
= NULL
;
380 *entry
= (*entry
)->next
;
387 infpy_threads (PyObject
*self
, PyObject
*args
)
390 struct threadlist_entry
*entry
;
391 inferior_object
*inf_obj
= (inferior_object
*) self
;
394 INFPY_REQUIRE_VALID (inf_obj
);
398 update_thread_list ();
400 catch (const gdb_exception
&except
)
402 GDB_PY_HANDLE_EXCEPTION (except
);
405 tuple
= PyTuple_New (inf_obj
->nthreads
);
409 for (i
= 0, entry
= inf_obj
->threads
; i
< inf_obj
->nthreads
;
410 i
++, entry
= entry
->next
)
412 PyObject
*thr
= (PyObject
*) entry
->thread_obj
.get ();
414 PyTuple_SET_ITEM (tuple
, i
, thr
);
421 infpy_get_num (PyObject
*self
, void *closure
)
423 inferior_object
*inf
= (inferior_object
*) self
;
425 INFPY_REQUIRE_VALID (inf
);
427 return gdb_py_object_from_longest (inf
->inferior
->num
).release ();
430 /* Return the connection number of the given inferior, or None if a
431 connection does not exist. */
434 infpy_get_connection_num (PyObject
*self
, void *closure
)
436 inferior_object
*inf
= (inferior_object
*) self
;
438 INFPY_REQUIRE_VALID (inf
);
440 process_stratum_target
*target
= inf
->inferior
->process_target ();
441 if (target
== nullptr)
444 return gdb_py_object_from_longest (target
->connection_number
).release ();
448 infpy_get_pid (PyObject
*self
, void *closure
)
450 inferior_object
*inf
= (inferior_object
*) self
;
452 INFPY_REQUIRE_VALID (inf
);
454 return gdb_py_object_from_longest (inf
->inferior
->pid
).release ();
458 infpy_get_was_attached (PyObject
*self
, void *closure
)
460 inferior_object
*inf
= (inferior_object
*) self
;
462 INFPY_REQUIRE_VALID (inf
);
463 if (inf
->inferior
->attach_flag
)
468 /* Getter of gdb.Inferior.progspace. */
471 infpy_get_progspace (PyObject
*self
, void *closure
)
473 inferior_object
*inf
= (inferior_object
*) self
;
475 INFPY_REQUIRE_VALID (inf
);
477 program_space
*pspace
= inf
->inferior
->pspace
;
478 gdb_assert (pspace
!= nullptr);
480 return pspace_to_pspace_object (pspace
).release ();
483 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
484 Returns a tuple of all inferiors. */
486 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
488 gdbpy_ref
<> list (PyList_New (0));
492 for (inferior
*inf
: all_inferiors ())
494 gdbpy_ref
<inferior_object
> inferior
= inferior_to_inferior_object (inf
);
496 if (inferior
== NULL
)
499 if (PyList_Append (list
.get (), (PyObject
*) inferior
.get ()) != 0)
503 return PyList_AsTuple (list
.get ());
506 /* Membuf and memory manipulation. */
508 /* Implementation of Inferior.read_memory (address, length).
509 Returns a Python buffer object with LENGTH bytes of the inferior's
510 memory at ADDRESS. Both arguments are integers. Returns NULL on error,
511 with a python exception set. */
513 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
515 CORE_ADDR addr
, length
;
516 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
517 PyObject
*addr_obj
, *length_obj
, *result
;
518 static const char *keywords
[] = { "address", "length", NULL
};
520 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
521 &addr_obj
, &length_obj
))
524 if (get_addr_from_python (addr_obj
, &addr
) < 0
525 || get_addr_from_python (length_obj
, &length
) < 0)
530 buffer
.reset ((gdb_byte
*) xmalloc (length
));
532 read_memory (addr
, buffer
.get (), length
);
534 catch (const gdb_exception
&except
)
536 GDB_PY_HANDLE_EXCEPTION (except
);
539 gdbpy_ref
<membuf_object
> membuf_obj (PyObject_New (membuf_object
,
540 &membuf_object_type
));
541 if (membuf_obj
== NULL
)
544 membuf_obj
->buffer
= buffer
.release ();
545 membuf_obj
->addr
= addr
;
546 membuf_obj
->length
= length
;
549 result
= PyMemoryView_FromObject ((PyObject
*) membuf_obj
.get ());
551 result
= PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
.get (), 0,
558 /* Implementation of Inferior.write_memory (address, buffer [, length]).
559 Writes the contents of BUFFER (a Python object supporting the read
560 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
561 bytes from BUFFER, or its entire contents if the argument is not
562 provided. The function returns nothing. Returns NULL on error, with
563 a python exception set. */
565 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
567 struct gdb_exception except
;
569 const gdb_byte
*buffer
;
570 CORE_ADDR addr
, length
;
571 PyObject
*addr_obj
, *length_obj
= NULL
;
572 static const char *keywords
[] = { "address", "buffer", "length", NULL
};
575 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "Os*|O", keywords
,
576 &addr_obj
, &pybuf
, &length_obj
))
579 Py_buffer_up
buffer_up (&pybuf
);
580 buffer
= (const gdb_byte
*) pybuf
.buf
;
583 if (get_addr_from_python (addr_obj
, &addr
) < 0)
588 else if (get_addr_from_python (length_obj
, &length
) < 0)
593 write_memory_with_notification (addr
, buffer
, length
);
595 catch (gdb_exception
&ex
)
597 except
= std::move (ex
);
600 GDB_PY_HANDLE_EXCEPTION (except
);
605 /* Destructor of Membuf objects. */
607 mbpy_dealloc (PyObject
*self
)
609 xfree (((membuf_object
*) self
)->buffer
);
610 Py_TYPE (self
)->tp_free (self
);
613 /* Return a description of the Membuf object. */
615 mbpy_str (PyObject
*self
)
617 membuf_object
*membuf_obj
= (membuf_object
*) self
;
619 return PyString_FromFormat (_("Memory buffer for address %s, \
620 which is %s bytes long."),
621 paddress (python_gdbarch
, membuf_obj
->addr
),
622 pulongest (membuf_obj
->length
));
628 get_buffer (PyObject
*self
, Py_buffer
*buf
, int flags
)
630 membuf_object
*membuf_obj
= (membuf_object
*) self
;
633 ret
= PyBuffer_FillInfo (buf
, self
, membuf_obj
->buffer
,
634 membuf_obj
->length
, 0,
637 /* Despite the documentation saying this field is a "const char *",
638 in Python 3.4 at least, it's really a "char *". */
639 buf
->format
= (char *) "c";
647 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
649 membuf_object
*membuf_obj
= (membuf_object
*) self
;
653 PyErr_SetString (PyExc_SystemError
,
654 _("The memory buffer supports only one segment."));
658 *ptrptr
= membuf_obj
->buffer
;
660 return membuf_obj
->length
;
664 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
666 return get_read_buffer (self
, segment
, ptrptr
);
670 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
673 *lenp
= ((membuf_object
*) self
)->length
;
679 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
684 ret
= get_read_buffer (self
, segment
, &ptr
);
685 *ptrptr
= (char *) ptr
;
693 gdb.search_memory (address, length, pattern). ADDRESS is the
694 address to start the search. LENGTH specifies the scope of the
695 search from ADDRESS. PATTERN is the pattern to search for (and
696 must be a Python object supporting the buffer protocol).
697 Returns a Python Long object holding the address where the pattern
698 was located, or if the pattern was not found, returns None. Returns NULL
699 on error, with a python exception set. */
701 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
703 struct gdb_exception except
;
704 CORE_ADDR start_addr
, length
;
705 static const char *keywords
[] = { "address", "length", "pattern", NULL
};
706 PyObject
*start_addr_obj
, *length_obj
;
707 Py_ssize_t pattern_size
;
708 const gdb_byte
*buffer
;
709 CORE_ADDR found_addr
;
713 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "OOs*", keywords
,
714 &start_addr_obj
, &length_obj
,
718 Py_buffer_up
buffer_up (&pybuf
);
719 buffer
= (const gdb_byte
*) pybuf
.buf
;
720 pattern_size
= pybuf
.len
;
722 if (get_addr_from_python (start_addr_obj
, &start_addr
) < 0)
725 if (get_addr_from_python (length_obj
, &length
) < 0)
730 PyErr_SetString (PyExc_ValueError
,
731 _("Search range is empty."));
734 /* Watch for overflows. */
735 else if (length
> CORE_ADDR_MAX
736 || (start_addr
+ length
- 1) < start_addr
)
738 PyErr_SetString (PyExc_ValueError
,
739 _("The search range is too large."));
745 found
= target_search_memory (start_addr
, length
,
746 buffer
, pattern_size
,
749 catch (gdb_exception
&ex
)
751 except
= std::move (ex
);
754 GDB_PY_HANDLE_EXCEPTION (except
);
757 return gdb_py_object_from_ulongest (found_addr
).release ();
762 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
763 Returns True if this inferior object still exists in GDB. */
766 infpy_is_valid (PyObject
*self
, PyObject
*args
)
768 inferior_object
*inf
= (inferior_object
*) self
;
776 /* Implementation of gdb.Inferior.thread_from_handle (self, handle)
777 -> gdb.InferiorThread. */
780 infpy_thread_from_thread_handle (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
782 PyObject
*handle_obj
;
783 inferior_object
*inf_obj
= (inferior_object
*) self
;
784 static const char *keywords
[] = { "handle", NULL
};
786 INFPY_REQUIRE_VALID (inf_obj
);
788 if (! gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "O", keywords
, &handle_obj
))
791 const gdb_byte
*bytes
;
793 Py_buffer_up buffer_up
;
796 if (PyObject_CheckBuffer (handle_obj
)
797 && PyObject_GetBuffer (handle_obj
, &py_buf
, PyBUF_SIMPLE
) == 0)
799 buffer_up
.reset (&py_buf
);
800 bytes
= (const gdb_byte
*) py_buf
.buf
;
801 bytes_len
= py_buf
.len
;
803 else if (gdbpy_is_value_object (handle_obj
))
805 struct value
*val
= value_object_to_value (handle_obj
);
806 bytes
= value_contents_all (val
);
807 bytes_len
= TYPE_LENGTH (value_type (val
));
811 PyErr_SetString (PyExc_TypeError
,
812 _("Argument 'handle' must be a thread handle object."));
819 struct thread_info
*thread_info
;
821 thread_info
= find_thread_by_handle
822 (gdb::array_view
<const gdb_byte
> (bytes
, bytes_len
),
824 if (thread_info
!= NULL
)
825 return thread_to_thread_object (thread_info
).release ();
827 catch (const gdb_exception
&except
)
829 GDB_PY_HANDLE_EXCEPTION (except
);
835 /* Implementation of gdb.Inferior.architecture. */
838 infpy_architecture (PyObject
*self
, PyObject
*args
)
840 inferior_object
*inf
= (inferior_object
*) self
;
842 INFPY_REQUIRE_VALID (inf
);
844 return gdbarch_to_arch_object (inf
->inferior
->gdbarch
);
847 /* Implement repr() for gdb.Inferior. */
850 infpy_repr (PyObject
*obj
)
852 inferior_object
*self
= (inferior_object
*) obj
;
853 inferior
*inf
= self
->inferior
;
856 return PyString_FromString ("<gdb.Inferior (invalid)>");
858 return PyString_FromFormat ("<gdb.Inferior num=%d, pid=%d>",
864 infpy_dealloc (PyObject
*obj
)
866 inferior_object
*inf_obj
= (inferior_object
*) obj
;
867 struct inferior
*inf
= inf_obj
->inferior
;
872 set_inferior_data (inf
, infpy_inf_data_key
, NULL
);
873 Py_TYPE (obj
)->tp_free (obj
);
876 /* Clear the INFERIOR pointer in an Inferior object and clear the
879 py_free_inferior (struct inferior
*inf
, void *datum
)
881 struct threadlist_entry
*th_entry
, *th_tmp
;
883 if (!gdb_python_initialized
)
886 gdbpy_enter
enter_py (python_gdbarch
, python_language
);
887 gdbpy_ref
<inferior_object
> inf_obj ((inferior_object
*) datum
);
889 inf_obj
->inferior
= NULL
;
891 /* Deallocate threads list. */
892 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
895 th_entry
= th_entry
->next
;
899 inf_obj
->nthreads
= 0;
902 /* Implementation of gdb.selected_inferior() -> gdb.Inferior.
903 Returns the current inferior object. */
906 gdbpy_selected_inferior (PyObject
*self
, PyObject
*args
)
909 inferior_to_inferior_object (current_inferior ()).release ());
912 void _initialize_py_inferior ();
914 _initialize_py_inferior ()
917 register_inferior_data_with_cleanup (NULL
, py_free_inferior
);
921 gdbpy_initialize_inferior (void)
923 if (PyType_Ready (&inferior_object_type
) < 0)
926 if (gdb_pymodule_addobject (gdb_module
, "Inferior",
927 (PyObject
*) &inferior_object_type
) < 0)
930 gdb::observers::new_thread
.attach (add_thread_object
, "py-inferior");
931 gdb::observers::thread_exit
.attach (delete_thread_object
, "py-inferior");
932 gdb::observers::normal_stop
.attach (python_on_normal_stop
, "py-inferior");
933 gdb::observers::target_resumed
.attach (python_on_resume
, "py-inferior");
934 gdb::observers::inferior_call_pre
.attach (python_on_inferior_call_pre
,
936 gdb::observers::inferior_call_post
.attach (python_on_inferior_call_post
,
938 gdb::observers::memory_changed
.attach (python_on_memory_change
,
940 gdb::observers::register_changed
.attach (python_on_register_change
,
942 gdb::observers::inferior_exit
.attach (python_inferior_exit
, "py-inferior");
943 /* Need to run after auto-load's new_objfile observer, so that
944 auto-loaded pretty-printers are available. */
945 gdb::observers::new_objfile
.attach
946 (python_new_objfile
, "py-inferior",
947 { &auto_load_new_objfile_observer_token
});
948 gdb::observers::inferior_added
.attach (python_new_inferior
, "py-inferior");
949 gdb::observers::inferior_removed
.attach (python_inferior_deleted
,
952 membuf_object_type
.tp_new
= PyType_GenericNew
;
953 if (PyType_Ready (&membuf_object_type
) < 0)
956 return gdb_pymodule_addobject (gdb_module
, "Membuf",
957 (PyObject
*) &membuf_object_type
);
960 static gdb_PyGetSetDef inferior_object_getset
[] =
962 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
963 { "connection_num", infpy_get_connection_num
, NULL
,
964 "ID of inferior's connection, as assigned by GDB.", NULL
},
965 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
967 { "was_attached", infpy_get_was_attached
, NULL
,
968 "True if the inferior was created using 'attach'.", NULL
},
969 { "progspace", infpy_get_progspace
, NULL
, "Program space of this inferior" },
973 static PyMethodDef inferior_object_methods
[] =
975 { "is_valid", infpy_is_valid
, METH_NOARGS
,
976 "is_valid () -> Boolean.\n\
977 Return true if this inferior is valid, false if not." },
978 { "threads", infpy_threads
, METH_NOARGS
,
979 "Return all the threads of this inferior." },
980 { "read_memory", (PyCFunction
) infpy_read_memory
,
981 METH_VARARGS
| METH_KEYWORDS
,
982 "read_memory (address, length) -> buffer\n\
983 Return a buffer object for reading from the inferior's memory." },
984 { "write_memory", (PyCFunction
) infpy_write_memory
,
985 METH_VARARGS
| METH_KEYWORDS
,
986 "write_memory (address, buffer [, length])\n\
987 Write the given buffer object to the inferior's memory." },
988 { "search_memory", (PyCFunction
) infpy_search_memory
,
989 METH_VARARGS
| METH_KEYWORDS
,
990 "search_memory (address, length, pattern) -> long\n\
991 Return a long with the address of a match, or None." },
992 /* thread_from_thread_handle is deprecated. */
993 { "thread_from_thread_handle", (PyCFunction
) infpy_thread_from_thread_handle
,
994 METH_VARARGS
| METH_KEYWORDS
,
995 "thread_from_thread_handle (handle) -> gdb.InferiorThread.\n\
996 Return thread object corresponding to thread handle.\n\
997 This method is deprecated - use thread_from_handle instead." },
998 { "thread_from_handle", (PyCFunction
) infpy_thread_from_thread_handle
,
999 METH_VARARGS
| METH_KEYWORDS
,
1000 "thread_from_handle (handle) -> gdb.InferiorThread.\n\
1001 Return thread object corresponding to thread handle." },
1002 { "architecture", (PyCFunction
) infpy_architecture
, METH_NOARGS
,
1003 "architecture () -> gdb.Architecture\n\
1004 Return architecture of this inferior." },
1008 PyTypeObject inferior_object_type
=
1010 PyVarObject_HEAD_INIT (NULL
, 0)
1011 "gdb.Inferior", /* tp_name */
1012 sizeof (inferior_object
), /* tp_basicsize */
1013 0, /* tp_itemsize */
1014 infpy_dealloc
, /* tp_dealloc */
1019 infpy_repr
, /* tp_repr */
1020 0, /* tp_as_number */
1021 0, /* tp_as_sequence */
1022 0, /* tp_as_mapping */
1026 0, /* tp_getattro */
1027 0, /* tp_setattro */
1028 0, /* tp_as_buffer */
1029 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
1030 "GDB inferior object", /* tp_doc */
1031 0, /* tp_traverse */
1033 0, /* tp_richcompare */
1034 0, /* tp_weaklistoffset */
1036 0, /* tp_iternext */
1037 inferior_object_methods
, /* tp_methods */
1039 inferior_object_getset
, /* tp_getset */
1042 0, /* tp_descr_get */
1043 0, /* tp_descr_set */
1044 0, /* tp_dictoffset */
1051 static PyBufferProcs buffer_procs
=
1058 static PyBufferProcs buffer_procs
= {
1064 #endif /* IS_PY3K */
1066 PyTypeObject membuf_object_type
= {
1067 PyVarObject_HEAD_INIT (NULL
, 0)
1068 "gdb.Membuf", /*tp_name*/
1069 sizeof (membuf_object
), /*tp_basicsize*/
1071 mbpy_dealloc
, /*tp_dealloc*/
1078 0, /*tp_as_sequence*/
1079 0, /*tp_as_mapping*/
1082 mbpy_str
, /*tp_str*/
1085 &buffer_procs
, /*tp_as_buffer*/
1086 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
1087 "GDB memory buffer object", /*tp_doc*/
1088 0, /* tp_traverse */
1090 0, /* tp_richcompare */
1091 0, /* tp_weaklistoffset */
1093 0, /* tp_iternext */
1099 0, /* tp_descr_get */
1100 0, /* tp_descr_set */
1101 0, /* tp_dictoffset */