1 /* Python interface to inferiors.
3 Copyright (C) 2009, 2010, 2011 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 "exceptions.h"
23 #include "gdbthread.h"
27 #include "python-internal.h"
28 #include "arch-utils.h"
30 #include "gdb_signals.h"
32 #include "py-stopevent.h"
34 struct threadlist_entry
{
35 thread_object
*thread_obj
;
36 struct threadlist_entry
*next
;
43 /* The inferior we represent. */
44 struct inferior
*inferior
;
46 /* thread_object instances under this inferior. This list owns a
47 reference to each object it contains. */
48 struct threadlist_entry
*threads
;
50 /* Number of threads in the list. */
54 static PyTypeObject inferior_object_type
;
56 static const struct inferior_data
*infpy_inf_data_key
;
62 /* These are kept just for mbpy_str. */
67 static PyTypeObject membuf_object_type
;
69 /* Require that INFERIOR be a valid inferior ID. */
70 #define INFPY_REQUIRE_VALID(Inferior) \
72 if (!Inferior->inferior) \
74 PyErr_SetString (PyExc_RuntimeError, \
75 _("Inferior no longer exists.")); \
81 python_on_normal_stop (struct bpstats
*bs
, int print_frame
)
83 struct cleanup
*cleanup
;
84 enum target_signal stop_signal
;
86 if (!find_thread_ptid (inferior_ptid
))
89 stop_signal
= inferior_thread ()->suspend
.stop_signal
;
91 cleanup
= ensure_python_env (get_current_arch (), current_language
);
93 if (emit_stop_event (bs
, stop_signal
) < 0)
96 do_cleanups (cleanup
);
100 python_on_resume (ptid_t ptid
)
102 struct cleanup
*cleanup
;
104 cleanup
= ensure_python_env (target_gdbarch
, current_language
);
106 if (emit_continue_event (ptid
) < 0)
107 gdbpy_print_stack ();
109 do_cleanups (cleanup
);
113 python_inferior_exit (struct inferior
*inf
)
115 struct cleanup
*cleanup
;
116 const LONGEST
*exit_code
= NULL
;
118 cleanup
= ensure_python_env (target_gdbarch
, current_language
);
120 if (inf
->has_exit_code
)
121 exit_code
= &inf
->exit_code
;
123 if (emit_exited_event (exit_code
, inf
) < 0)
124 gdbpy_print_stack ();
126 do_cleanups (cleanup
);
129 /* Callback used to notify Python listeners about new objfiles loaded in the
133 python_new_objfile (struct objfile
*objfile
)
135 struct cleanup
*cleanup
;
140 cleanup
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
142 if (emit_new_objfile_event (objfile
) < 0)
143 gdbpy_print_stack ();
145 do_cleanups (cleanup
);
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. */
153 inferior_to_inferior_object (struct inferior
*inferior
)
155 inferior_object
*inf_obj
;
157 inf_obj
= inferior_data (inferior
, infpy_inf_data_key
);
160 struct cleanup
*cleanup
;
161 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
163 inf_obj
= PyObject_New (inferior_object
, &inferior_object_type
);
166 do_cleanups (cleanup
);
170 inf_obj
->inferior
= inferior
;
171 inf_obj
->threads
= NULL
;
172 inf_obj
->nthreads
= 0;
174 set_inferior_data (inferior
, infpy_inf_data_key
, inf_obj
);
176 do_cleanups (cleanup
);
179 Py_INCREF ((PyObject
*)inf_obj
);
181 return (PyObject
*) inf_obj
;
184 /* Finds the Python Inferior object for the given PID. Returns a
185 reference, or NULL if PID does not match any inferior object. */
188 find_inferior_object (int pid
)
190 struct inflist_entry
*p
;
191 struct inferior
*inf
= find_inferior_pid (pid
);
194 return inferior_to_inferior_object (inf
);
200 find_thread_object (ptid_t ptid
)
203 struct threadlist_entry
*thread
;
205 thread_object
*found
= NULL
;
211 inf_obj
= find_inferior_object (pid
);
216 for (thread
= ((inferior_object
*)inf_obj
)->threads
; thread
;
217 thread
= thread
->next
)
218 if (ptid_equal (thread
->thread_obj
->thread
->ptid
, ptid
))
220 found
= thread
->thread_obj
;
233 add_thread_object (struct thread_info
*tp
)
235 struct cleanup
*cleanup
;
236 thread_object
*thread_obj
;
237 inferior_object
*inf_obj
;
238 struct threadlist_entry
*entry
;
240 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
242 thread_obj
= create_thread_object (tp
);
245 gdbpy_print_stack ();
246 do_cleanups (cleanup
);
250 inf_obj
= (inferior_object
*) thread_obj
->inf_obj
;
252 entry
= xmalloc (sizeof (struct threadlist_entry
));
253 entry
->thread_obj
= thread_obj
;
254 entry
->next
= inf_obj
->threads
;
256 inf_obj
->threads
= entry
;
259 do_cleanups (cleanup
);
263 delete_thread_object (struct thread_info
*tp
, int ignore
)
265 struct cleanup
*cleanup
;
266 inferior_object
*inf_obj
;
267 thread_object
*thread_obj
;
268 struct threadlist_entry
**entry
, *tmp
;
270 inf_obj
= (inferior_object
*) find_inferior_object (PIDGET(tp
->ptid
));
274 /* Find thread entry in its inferior's thread_list. */
275 for (entry
= &inf_obj
->threads
; *entry
!= NULL
; entry
=
277 if ((*entry
)->thread_obj
->thread
== tp
)
286 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
289 tmp
->thread_obj
->thread
= NULL
;
291 *entry
= (*entry
)->next
;
294 Py_DECREF (tmp
->thread_obj
);
298 do_cleanups (cleanup
);
302 infpy_threads (PyObject
*self
, PyObject
*args
)
305 struct threadlist_entry
*entry
;
306 inferior_object
*inf_obj
= (inferior_object
*) self
;
309 INFPY_REQUIRE_VALID (inf_obj
);
311 tuple
= PyTuple_New (inf_obj
->nthreads
);
315 for (i
= 0, entry
= inf_obj
->threads
; i
< inf_obj
->nthreads
;
316 i
++, entry
= entry
->next
)
318 Py_INCREF (entry
->thread_obj
);
319 PyTuple_SET_ITEM (tuple
, i
, (PyObject
*) entry
->thread_obj
);
326 infpy_get_num (PyObject
*self
, void *closure
)
328 inferior_object
*inf
= (inferior_object
*) self
;
330 INFPY_REQUIRE_VALID (inf
);
332 return PyLong_FromLong (inf
->inferior
->num
);
336 infpy_get_pid (PyObject
*self
, void *closure
)
338 inferior_object
*inf
= (inferior_object
*) self
;
340 INFPY_REQUIRE_VALID (inf
);
342 return PyLong_FromLong (inf
->inferior
->pid
);
346 infpy_get_was_attached (PyObject
*self
, void *closure
)
348 inferior_object
*inf
= (inferior_object
*) self
;
350 INFPY_REQUIRE_VALID (inf
);
351 if (inf
->inferior
->attach_flag
)
357 build_inferior_list (struct inferior
*inf
, void *arg
)
359 PyObject
*list
= arg
;
360 PyObject
*inferior
= inferior_to_inferior_object (inf
);
366 success
= PyList_Append (list
, inferior
);
367 Py_DECREF (inferior
);
375 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
376 Returns a tuple of all inferiors. */
378 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
381 PyObject
*list
, *inferior
;
382 struct inferior
*inf
;
384 list
= PyList_New (0);
388 if (iterate_over_inferiors (build_inferior_list
, list
))
394 return PyList_AsTuple (list
);
397 /* Membuf and memory manipulation. */
399 /* Implementation of gdb.read_memory (address, length).
400 Returns a Python buffer object with LENGTH bytes of the inferior's
401 memory at ADDRESS. Both arguments are integers. Returns NULL on error,
402 with a python exception set. */
404 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
407 CORE_ADDR addr
, length
;
409 membuf_object
*membuf_obj
;
410 PyObject
*addr_obj
, *length_obj
;
411 struct cleanup
*cleanups
;
412 volatile struct gdb_exception except
;
413 static char *keywords
[] = { "address", "length", NULL
};
415 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
416 &addr_obj
, &length_obj
))
419 cleanups
= make_cleanup (null_cleanup
, NULL
);
421 TRY_CATCH (except
, RETURN_MASK_ALL
)
423 if (!get_addr_from_python (addr_obj
, &addr
)
424 || !get_addr_from_python (length_obj
, &length
))
430 buffer
= xmalloc (length
);
431 make_cleanup (xfree
, buffer
);
433 read_memory (addr
, buffer
, length
);
435 if (except
.reason
< 0)
437 do_cleanups (cleanups
);
438 GDB_PY_HANDLE_EXCEPTION (except
);
443 do_cleanups (cleanups
);
447 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
448 if (membuf_obj
== NULL
)
450 PyErr_SetString (PyExc_MemoryError
,
451 _("Could not allocate memory buffer object."));
452 do_cleanups (cleanups
);
456 discard_cleanups (cleanups
);
458 membuf_obj
->buffer
= buffer
;
459 membuf_obj
->addr
= addr
;
460 membuf_obj
->length
= length
;
462 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
466 /* Implementation of gdb.write_memory (address, buffer [, length]).
467 Writes the contents of BUFFER (a Python object supporting the read
468 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
469 bytes from BUFFER, or its entire contents if the argument is not
470 provided. The function returns nothing. Returns NULL on error, with
471 a python exception set. */
473 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
478 CORE_ADDR addr
, length
;
479 PyObject
*addr_obj
, *length_obj
= NULL
;
480 volatile struct gdb_exception except
;
481 static char *keywords
[] = { "address", "buffer", "length", NULL
};
484 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
485 &addr_obj
, &buffer
, &buf_len
,
489 TRY_CATCH (except
, RETURN_MASK_ALL
)
491 if (!get_addr_from_python (addr_obj
, &addr
))
499 else if (!get_addr_from_python (length_obj
, &length
))
504 write_memory (addr
, buffer
, length
);
506 GDB_PY_HANDLE_EXCEPTION (except
);
514 /* Destructor of Membuf objects. */
516 mbpy_dealloc (PyObject
*self
)
518 xfree (((membuf_object
*) self
)->buffer
);
519 self
->ob_type
->tp_free (self
);
522 /* Return a description of the Membuf object. */
524 mbpy_str (PyObject
*self
)
526 membuf_object
*membuf_obj
= (membuf_object
*) self
;
528 return PyString_FromFormat (_("Memory buffer for address %s, \
529 which is %s bytes long."),
530 paddress (python_gdbarch
, membuf_obj
->addr
),
531 pulongest (membuf_obj
->length
));
535 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
537 membuf_object
*membuf_obj
= (membuf_object
*) self
;
541 PyErr_SetString (PyExc_SystemError
,
542 _("The memory buffer supports only one segment."));
546 *ptrptr
= membuf_obj
->buffer
;
548 return membuf_obj
->length
;
552 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
554 return get_read_buffer (self
, segment
, ptrptr
);
558 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
561 *lenp
= ((membuf_object
*) self
)->length
;
567 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
572 ret
= get_read_buffer (self
, segment
, &ptr
);
573 *ptrptr
= (char *) ptr
;
579 gdb.search_memory (address, length, pattern). ADDRESS is the
580 address to start the search. LENGTH specifies the scope of the
581 search from ADDRESS. PATTERN is the pattern to search for (and
582 must be a Python object supporting the buffer protocol).
583 Returns a Python Long object holding the address where the pattern
584 was located, or if the pattern was not found, returns None. Returns NULL
585 on error, with a python exception set. */
587 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
589 CORE_ADDR start_addr
, length
;
590 static char *keywords
[] = { "address", "length", "pattern", NULL
};
591 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
592 volatile struct gdb_exception except
;
593 Py_ssize_t pattern_size
;
595 CORE_ADDR found_addr
;
598 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
599 &start_addr_obj
, &length_obj
,
603 if (get_addr_from_python (start_addr_obj
, &start_addr
)
604 && get_addr_from_python (length_obj
, &length
))
608 PyErr_SetString (PyExc_ValueError
,
609 _("Search range is empty."));
612 /* Watch for overflows. */
613 else if (length
> CORE_ADDR_MAX
614 || (start_addr
+ length
- 1) < start_addr
)
616 PyErr_SetString (PyExc_ValueError
,
617 _("The search range is too large."));
625 if (!PyObject_CheckReadBuffer (pattern
))
627 PyErr_SetString (PyExc_RuntimeError
,
628 _("The pattern is not a Python buffer."));
633 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
636 TRY_CATCH (except
, RETURN_MASK_ALL
)
638 found
= target_search_memory (start_addr
, length
,
639 buffer
, pattern_size
,
642 GDB_PY_HANDLE_EXCEPTION (except
);
645 return PyLong_FromLong (found_addr
);
650 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
651 Returns True if this inferior object still exists in GDB. */
654 infpy_is_valid (PyObject
*self
, PyObject
*args
)
656 inferior_object
*inf
= (inferior_object
*) self
;
665 infpy_dealloc (PyObject
*obj
)
667 inferior_object
*inf_obj
= (inferior_object
*) obj
;
668 struct inferior
*inf
= inf_obj
->inferior
;
673 set_inferior_data (inf
, infpy_inf_data_key
, NULL
);
676 /* Clear the INFERIOR pointer in an Inferior object and clear the
679 py_free_inferior (struct inferior
*inf
, void *datum
)
682 struct cleanup
*cleanup
;
683 inferior_object
*inf_obj
= datum
;
684 struct threadlist_entry
*th_entry
, *th_tmp
;
686 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
688 inf_obj
->inferior
= NULL
;
690 /* Deallocate threads list. */
691 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
693 Py_DECREF (th_entry
->thread_obj
);
696 th_entry
= th_entry
->next
;
700 inf_obj
->nthreads
= 0;
702 Py_DECREF ((PyObject
*) inf_obj
);
703 do_cleanups (cleanup
);
706 /* Implementation of gdb.selected_inferior() -> gdb.Inferior.
707 Returns the current inferior object. */
710 gdbpy_selected_inferior (PyObject
*self
, PyObject
*args
)
714 inf_obj
= inferior_to_inferior_object (current_inferior ());
721 gdbpy_initialize_inferior (void)
723 if (PyType_Ready (&inferior_object_type
) < 0)
726 Py_INCREF (&inferior_object_type
);
727 PyModule_AddObject (gdb_module
, "Inferior",
728 (PyObject
*) &inferior_object_type
);
731 register_inferior_data_with_cleanup (py_free_inferior
);
733 observer_attach_new_thread (add_thread_object
);
734 observer_attach_thread_exit (delete_thread_object
);
735 observer_attach_normal_stop (python_on_normal_stop
);
736 observer_attach_target_resumed (python_on_resume
);
737 observer_attach_inferior_exit (python_inferior_exit
);
738 observer_attach_new_objfile (python_new_objfile
);
740 membuf_object_type
.tp_new
= PyType_GenericNew
;
741 if (PyType_Ready (&membuf_object_type
) < 0)
744 Py_INCREF (&membuf_object_type
);
745 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
746 &membuf_object_type
);
749 static PyGetSetDef inferior_object_getset
[] =
751 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
752 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
754 { "was_attached", infpy_get_was_attached
, NULL
,
755 "True if the inferior was created using 'attach'.", NULL
},
759 static PyMethodDef inferior_object_methods
[] =
761 { "is_valid", infpy_is_valid
, METH_NOARGS
,
762 "is_valid () -> Boolean.\n\
763 Return true if this inferior is valid, false if not." },
764 { "threads", infpy_threads
, METH_NOARGS
,
765 "Return all the threads of this inferior." },
766 { "read_memory", (PyCFunction
) infpy_read_memory
,
767 METH_VARARGS
| METH_KEYWORDS
,
768 "read_memory (address, length) -> buffer\n\
769 Return a buffer object for reading from the inferior's memory." },
770 { "write_memory", (PyCFunction
) infpy_write_memory
,
771 METH_VARARGS
| METH_KEYWORDS
,
772 "write_memory (address, buffer [, length])\n\
773 Write the given buffer object to the inferior's memory." },
774 { "search_memory", (PyCFunction
) infpy_search_memory
,
775 METH_VARARGS
| METH_KEYWORDS
,
776 "search_memory (address, length, pattern) -> long\n\
777 Return a long with the address of a match, or None." },
781 static PyTypeObject inferior_object_type
=
783 PyObject_HEAD_INIT (NULL
)
785 "gdb.Inferior", /* tp_name */
786 sizeof (inferior_object
), /* tp_basicsize */
788 infpy_dealloc
, /* tp_dealloc */
794 0, /* tp_as_number */
795 0, /* tp_as_sequence */
796 0, /* tp_as_mapping */
802 0, /* tp_as_buffer */
803 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
804 "GDB inferior object", /* tp_doc */
807 0, /* tp_richcompare */
808 0, /* tp_weaklistoffset */
811 inferior_object_methods
, /* tp_methods */
813 inferior_object_getset
, /* tp_getset */
816 0, /* tp_descr_get */
817 0, /* tp_descr_set */
818 0, /* tp_dictoffset */
823 /* Python doesn't provide a decent way to get compatibility here. */
824 #if HAVE_LIBPYTHON2_4
825 #define CHARBUFFERPROC_NAME getcharbufferproc
827 #define CHARBUFFERPROC_NAME charbufferproc
830 static PyBufferProcs buffer_procs
= {
834 /* The cast here works around a difference between Python 2.4 and
836 (CHARBUFFERPROC_NAME
) get_char_buffer
839 static PyTypeObject membuf_object_type
= {
840 PyObject_HEAD_INIT (NULL
)
842 "gdb.Membuf", /*tp_name*/
843 sizeof (membuf_object
), /*tp_basicsize*/
845 mbpy_dealloc
, /*tp_dealloc*/
852 0, /*tp_as_sequence*/
859 &buffer_procs
, /*tp_as_buffer*/
860 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
861 "GDB memory buffer object", /*tp_doc*/
864 0, /* tp_richcompare */
865 0, /* tp_weaklistoffset */
873 0, /* tp_descr_get */
874 0, /* tp_descr_set */
875 0, /* tp_dictoffset */