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"
26 #include "python-internal.h"
27 #include "arch-utils.h"
29 #include "gdb_signals.h"
31 #include "py-stopevent.h"
33 struct threadlist_entry
{
34 thread_object
*thread_obj
;
35 struct threadlist_entry
*next
;
42 /* The inferior we represent. */
43 struct inferior
*inferior
;
45 /* thread_object instances under this inferior. This list owns a
46 reference to each object it contains. */
47 struct threadlist_entry
*threads
;
49 /* Number of threads in the list. */
53 static PyTypeObject inferior_object_type
;
55 static const struct inferior_data
*infpy_inf_data_key
;
61 /* These are kept just for mbpy_str. */
66 static PyTypeObject membuf_object_type
;
68 /* Require that INFERIOR be a valid inferior ID. */
69 #define INFPY_REQUIRE_VALID(Inferior) \
71 if (!Inferior->inferior) \
73 PyErr_SetString (PyExc_RuntimeError, \
74 _("Inferior no longer exists.")); \
80 python_on_normal_stop (struct bpstats
*bs
, int print_frame
)
82 struct cleanup
*cleanup
;
83 enum target_signal stop_signal
;
85 if (!find_thread_ptid (inferior_ptid
))
88 stop_signal
= inferior_thread ()->suspend
.stop_signal
;
90 cleanup
= ensure_python_env (get_current_arch (), current_language
);
92 if (emit_stop_event (bs
, stop_signal
) < 0)
95 do_cleanups (cleanup
);
99 python_on_resume (ptid_t ptid
)
101 struct cleanup
*cleanup
;
103 cleanup
= ensure_python_env (get_current_arch (), current_language
);
105 if (emit_continue_event (ptid
) < 0)
106 gdbpy_print_stack ();
108 do_cleanups (cleanup
);
112 python_inferior_exit (struct inferior
*inf
)
114 struct cleanup
*cleanup
;
115 LONGEST exit_code
= -1;
117 struct target_waitstatus status
;
119 cleanup
= ensure_python_env (get_current_arch (), current_language
);
121 get_last_target_status (&ptidp
, &status
);
123 exit_code
= status
.value
.integer
;
126 && emit_exited_event (exit_code
) < 0)
127 gdbpy_print_stack ();
129 do_cleanups (cleanup
);
132 /* Return a borrowed reference to the Python object of type Inferior
133 representing INFERIOR. If the object has already been created,
134 return it, otherwise, create it. Return NULL on failure. */
136 inferior_to_inferior_object (struct inferior
*inferior
)
138 inferior_object
*inf_obj
;
140 inf_obj
= inferior_data (inferior
, infpy_inf_data_key
);
143 struct cleanup
*cleanup
;
144 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
146 inf_obj
= PyObject_New (inferior_object
, &inferior_object_type
);
149 do_cleanups (cleanup
);
153 inf_obj
->inferior
= inferior
;
154 inf_obj
->threads
= NULL
;
155 inf_obj
->nthreads
= 0;
157 set_inferior_data (inferior
, infpy_inf_data_key
, inf_obj
);
159 do_cleanups (cleanup
);
162 return (PyObject
*) inf_obj
;
165 /* Finds the Python Inferior object for the given PID. Returns a
166 borrowed reference, or NULL if PID does not match any inferior
170 find_inferior_object (int pid
)
172 struct inflist_entry
*p
;
173 struct inferior
*inf
= find_inferior_pid (pid
);
176 return inferior_to_inferior_object (inf
);
182 find_thread_object (ptid_t ptid
)
185 struct threadlist_entry
*thread
;
192 inf_obj
= find_inferior_object (pid
);
195 for (thread
= ((inferior_object
*)inf_obj
)->threads
; thread
;
196 thread
= thread
->next
)
197 if (ptid_equal (thread
->thread_obj
->thread
->ptid
, ptid
))
198 return thread
->thread_obj
;
204 add_thread_object (struct thread_info
*tp
)
206 struct cleanup
*cleanup
;
207 thread_object
*thread_obj
;
208 inferior_object
*inf_obj
;
209 struct threadlist_entry
*entry
;
211 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
213 thread_obj
= create_thread_object (tp
);
216 gdbpy_print_stack ();
217 do_cleanups (cleanup
);
221 inf_obj
= (inferior_object
*) thread_obj
->inf_obj
;
223 entry
= xmalloc (sizeof (struct threadlist_entry
));
224 entry
->thread_obj
= thread_obj
;
225 entry
->next
= inf_obj
->threads
;
227 inf_obj
->threads
= entry
;
230 do_cleanups (cleanup
);
234 delete_thread_object (struct thread_info
*tp
, int ignore
)
236 struct cleanup
*cleanup
;
237 inferior_object
*inf_obj
;
238 thread_object
*thread_obj
;
239 struct threadlist_entry
**entry
, *tmp
;
241 inf_obj
= (inferior_object
*) find_inferior_object (PIDGET(tp
->ptid
));
245 /* Find thread entry in its inferior's thread_list. */
246 for (entry
= &inf_obj
->threads
; *entry
!= NULL
; entry
=
248 if ((*entry
)->thread_obj
->thread
== tp
)
254 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
257 tmp
->thread_obj
->thread
= NULL
;
259 *entry
= (*entry
)->next
;
262 Py_DECREF (tmp
->thread_obj
);
265 do_cleanups (cleanup
);
269 infpy_threads (PyObject
*self
, PyObject
*args
)
272 struct threadlist_entry
*entry
;
273 inferior_object
*inf_obj
= (inferior_object
*) self
;
276 INFPY_REQUIRE_VALID (inf_obj
);
278 tuple
= PyTuple_New (inf_obj
->nthreads
);
282 for (i
= 0, entry
= inf_obj
->threads
; i
< inf_obj
->nthreads
;
283 i
++, entry
= entry
->next
)
285 Py_INCREF (entry
->thread_obj
);
286 PyTuple_SET_ITEM (tuple
, i
, (PyObject
*) entry
->thread_obj
);
293 infpy_get_num (PyObject
*self
, void *closure
)
295 inferior_object
*inf
= (inferior_object
*) self
;
297 INFPY_REQUIRE_VALID (inf
);
299 return PyLong_FromLong (inf
->inferior
->num
);
303 infpy_get_pid (PyObject
*self
, void *closure
)
305 inferior_object
*inf
= (inferior_object
*) self
;
307 INFPY_REQUIRE_VALID (inf
);
309 return PyLong_FromLong (inf
->inferior
->pid
);
313 infpy_get_was_attached (PyObject
*self
, void *closure
)
315 inferior_object
*inf
= (inferior_object
*) self
;
317 INFPY_REQUIRE_VALID (inf
);
318 if (inf
->inferior
->attach_flag
)
324 build_inferior_list (struct inferior
*inf
, void *arg
)
326 PyObject
*list
= arg
;
327 PyObject
*inferior
= inferior_to_inferior_object (inf
);
329 if (PyList_Append (list
, inferior
))
335 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
336 Returns a tuple of all inferiors. */
338 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
341 PyObject
*list
, *inferior
;
342 struct inferior
*inf
;
344 list
= PyList_New (0);
348 if (iterate_over_inferiors (build_inferior_list
, list
))
354 return PyList_AsTuple (list
);
357 /* Membuf and memory manipulation. */
359 /* Implementation of gdb.read_memory (address, length).
360 Returns a Python buffer object with LENGTH bytes of the inferior's
361 memory at ADDRESS. Both arguments are integers. Returns NULL on error,
362 with a python exception set. */
364 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
367 CORE_ADDR addr
, length
;
369 membuf_object
*membuf_obj
;
370 PyObject
*addr_obj
, *length_obj
;
371 struct cleanup
*cleanups
;
372 volatile struct gdb_exception except
;
373 static char *keywords
[] = { "address", "length", NULL
};
375 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
376 &addr_obj
, &length_obj
))
379 cleanups
= make_cleanup (null_cleanup
, NULL
);
381 TRY_CATCH (except
, RETURN_MASK_ALL
)
383 if (!get_addr_from_python (addr_obj
, &addr
)
384 || !get_addr_from_python (length_obj
, &length
))
390 buffer
= xmalloc (length
);
391 make_cleanup (xfree
, buffer
);
393 read_memory (addr
, buffer
, length
);
395 if (except
.reason
< 0)
397 do_cleanups (cleanups
);
398 GDB_PY_HANDLE_EXCEPTION (except
);
403 do_cleanups (cleanups
);
407 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
408 if (membuf_obj
== NULL
)
410 PyErr_SetString (PyExc_MemoryError
,
411 _("Could not allocate memory buffer object."));
412 do_cleanups (cleanups
);
416 discard_cleanups (cleanups
);
418 membuf_obj
->buffer
= buffer
;
419 membuf_obj
->addr
= addr
;
420 membuf_obj
->length
= length
;
422 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
426 /* Implementation of gdb.write_memory (address, buffer [, length]).
427 Writes the contents of BUFFER (a Python object supporting the read
428 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
429 bytes from BUFFER, or its entire contents if the argument is not
430 provided. The function returns nothing. Returns NULL on error, with
431 a python exception set. */
433 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
435 int buf_len
, error
= 0;
437 CORE_ADDR addr
, length
;
438 PyObject
*addr_obj
, *length_obj
= NULL
;
439 volatile struct gdb_exception except
;
440 static char *keywords
[] = { "address", "buffer", "length", NULL
};
443 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
444 &addr_obj
, &buffer
, &buf_len
,
448 TRY_CATCH (except
, RETURN_MASK_ALL
)
450 if (!get_addr_from_python (addr_obj
, &addr
))
458 else if (!get_addr_from_python (length_obj
, &length
))
463 write_memory (addr
, buffer
, length
);
465 GDB_PY_HANDLE_EXCEPTION (except
);
473 /* Destructor of Membuf objects. */
475 mbpy_dealloc (PyObject
*self
)
477 xfree (((membuf_object
*) self
)->buffer
);
478 self
->ob_type
->tp_free (self
);
481 /* Return a description of the Membuf object. */
483 mbpy_str (PyObject
*self
)
485 membuf_object
*membuf_obj
= (membuf_object
*) self
;
487 return PyString_FromFormat (_("Memory buffer for address %s, \
488 which is %s bytes long."),
489 paddress (python_gdbarch
, membuf_obj
->addr
),
490 pulongest (membuf_obj
->length
));
494 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
496 membuf_object
*membuf_obj
= (membuf_object
*) self
;
500 PyErr_SetString (PyExc_SystemError
,
501 _("The memory buffer supports only one segment."));
505 *ptrptr
= membuf_obj
->buffer
;
507 return membuf_obj
->length
;
511 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
513 return get_read_buffer (self
, segment
, ptrptr
);
517 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
520 *lenp
= ((membuf_object
*) self
)->length
;
526 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
531 ret
= get_read_buffer (self
, segment
, &ptr
);
532 *ptrptr
= (char *) ptr
;
538 gdb.search_memory (address, length, pattern). ADDRESS is the
539 address to start the search. LENGTH specifies the scope of the
540 search from ADDRESS. PATTERN is the pattern to search for (and
541 must be a Python object supporting the buffer protocol).
542 Returns a Python Long object holding the address where the pattern
543 was located, or if the pattern was not found, returns None. Returns NULL
544 on error, with a python exception set. */
546 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
548 CORE_ADDR start_addr
, length
;
549 static char *keywords
[] = { "address", "length", "pattern", NULL
};
550 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
551 volatile struct gdb_exception except
;
552 Py_ssize_t pattern_size
;
554 CORE_ADDR found_addr
;
557 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
558 &start_addr_obj
, &length_obj
,
562 if (get_addr_from_python (start_addr_obj
, &start_addr
)
563 && get_addr_from_python (length_obj
, &length
))
567 PyErr_SetString (PyExc_ValueError
,
568 _("Search range is empty."));
571 /* Watch for overflows. */
572 else if (length
> CORE_ADDR_MAX
573 || (start_addr
+ length
- 1) < start_addr
)
575 PyErr_SetString (PyExc_ValueError
,
576 _("The search range is too large."));
584 if (!PyObject_CheckReadBuffer (pattern
))
586 PyErr_SetString (PyExc_RuntimeError
,
587 _("The pattern is not a Python buffer."));
592 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
595 TRY_CATCH (except
, RETURN_MASK_ALL
)
597 found
= target_search_memory (start_addr
, length
,
598 buffer
, pattern_size
,
601 GDB_PY_HANDLE_EXCEPTION (except
);
604 return PyLong_FromLong (found_addr
);
609 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
610 Returns True if this inferior object still exists in GDB. */
613 infpy_is_valid (PyObject
*self
, PyObject
*args
)
615 inferior_object
*inf
= (inferior_object
*) self
;
624 /* Clear the INFERIOR pointer in an Inferior object and clear the
627 py_free_inferior (struct inferior
*inf
, void *datum
)
630 struct cleanup
*cleanup
;
631 inferior_object
*inf_obj
= datum
;
632 struct threadlist_entry
*th_entry
, *th_tmp
;
634 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
636 inf_obj
->inferior
= NULL
;
638 /* Deallocate threads list. */
639 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
641 Py_DECREF (th_entry
->thread_obj
);
644 th_entry
= th_entry
->next
;
648 inf_obj
->nthreads
= 0;
650 Py_DECREF ((PyObject
*) inf_obj
);
651 do_cleanups (cleanup
);
655 gdbpy_initialize_inferior (void)
657 if (PyType_Ready (&inferior_object_type
) < 0)
660 Py_INCREF (&inferior_object_type
);
661 PyModule_AddObject (gdb_module
, "Inferior",
662 (PyObject
*) &inferior_object_type
);
665 register_inferior_data_with_cleanup (py_free_inferior
);
667 observer_attach_new_thread (add_thread_object
);
668 observer_attach_thread_exit (delete_thread_object
);
669 observer_attach_normal_stop (python_on_normal_stop
);
670 observer_attach_target_resumed (python_on_resume
);
671 observer_attach_inferior_exit (python_inferior_exit
);
673 if (PyType_Ready (&membuf_object_type
) < 0)
676 Py_INCREF (&membuf_object_type
);
677 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
678 &membuf_object_type
);
681 static PyGetSetDef inferior_object_getset
[] =
683 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
684 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
686 { "was_attached", infpy_get_was_attached
, NULL
,
687 "True if the inferior was created using 'attach'.", NULL
},
691 static PyMethodDef inferior_object_methods
[] =
693 { "is_valid", infpy_is_valid
, METH_NOARGS
,
694 "is_valid () -> Boolean.\n\
695 Return true if this inferior is valid, false if not." },
696 { "threads", infpy_threads
, METH_NOARGS
,
697 "Return all the threads of this inferior." },
698 { "read_memory", (PyCFunction
) infpy_read_memory
,
699 METH_VARARGS
| METH_KEYWORDS
,
700 "read_memory (address, length) -> buffer\n\
701 Return a buffer object for reading from the inferior's memory." },
702 { "write_memory", (PyCFunction
) infpy_write_memory
,
703 METH_VARARGS
| METH_KEYWORDS
,
704 "write_memory (address, buffer [, length])\n\
705 Write the given buffer object to the inferior's memory." },
706 { "search_memory", (PyCFunction
) infpy_search_memory
,
707 METH_VARARGS
| METH_KEYWORDS
,
708 "search_memory (address, length, pattern) -> long\n\
709 Return a long with the address of a match, or None." },
713 static PyTypeObject inferior_object_type
=
715 PyObject_HEAD_INIT (NULL
)
717 "gdb.Inferior", /* tp_name */
718 sizeof (inferior_object
), /* tp_basicsize */
726 0, /* tp_as_number */
727 0, /* tp_as_sequence */
728 0, /* tp_as_mapping */
734 0, /* tp_as_buffer */
735 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
736 "GDB inferior object", /* tp_doc */
739 0, /* tp_richcompare */
740 0, /* tp_weaklistoffset */
743 inferior_object_methods
, /* tp_methods */
745 inferior_object_getset
, /* tp_getset */
748 0, /* tp_descr_get */
749 0, /* tp_descr_set */
750 0, /* tp_dictoffset */
755 /* Python doesn't provide a decent way to get compatibility here. */
756 #if HAVE_LIBPYTHON2_4
757 #define CHARBUFFERPROC_NAME getcharbufferproc
759 #define CHARBUFFERPROC_NAME charbufferproc
762 static PyBufferProcs buffer_procs
= {
766 /* The cast here works around a difference between Python 2.4 and
768 (CHARBUFFERPROC_NAME
) get_char_buffer
771 static PyTypeObject membuf_object_type
= {
772 PyObject_HEAD_INIT (NULL
)
774 "gdb.Membuf", /*tp_name*/
775 sizeof (membuf_object
), /*tp_basicsize*/
777 mbpy_dealloc
, /*tp_dealloc*/
784 0, /*tp_as_sequence*/
791 &buffer_procs
, /*tp_as_buffer*/
792 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
793 "GDB memory buffer object", /*tp_doc*/
796 0, /* tp_richcompare */
797 0, /* tp_weaklistoffset */
805 0, /* tp_descr_get */
806 0, /* tp_descr_set */
807 0, /* tp_dictoffset */
810 PyType_GenericNew
/* tp_new */