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 (target_gdbarch
, 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 const LONGEST
*exit_code
= NULL
;
117 cleanup
= ensure_python_env (target_gdbarch
, current_language
);
119 if (inf
->has_exit_code
)
120 exit_code
= &inf
->exit_code
;
122 if (emit_exited_event (exit_code
) < 0)
123 gdbpy_print_stack ();
125 do_cleanups (cleanup
);
128 /* Return a reference to the Python object of type Inferior
129 representing INFERIOR. If the object has already been created,
130 return it and increment the reference count, otherwise, create it.
131 Return NULL on failure. */
133 inferior_to_inferior_object (struct inferior
*inferior
)
135 inferior_object
*inf_obj
;
137 inf_obj
= inferior_data (inferior
, infpy_inf_data_key
);
140 struct cleanup
*cleanup
;
141 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
143 inf_obj
= PyObject_New (inferior_object
, &inferior_object_type
);
146 do_cleanups (cleanup
);
150 inf_obj
->inferior
= inferior
;
151 inf_obj
->threads
= NULL
;
152 inf_obj
->nthreads
= 0;
154 set_inferior_data (inferior
, infpy_inf_data_key
, inf_obj
);
156 do_cleanups (cleanup
);
159 Py_INCREF ((PyObject
*)inf_obj
);
161 return (PyObject
*) inf_obj
;
164 /* Finds the Python Inferior object for the given PID. Returns a
165 reference, or NULL if PID does not match any inferior object. */
168 find_inferior_object (int pid
)
170 struct inflist_entry
*p
;
171 struct inferior
*inf
= find_inferior_pid (pid
);
174 return inferior_to_inferior_object (inf
);
180 find_thread_object (ptid_t ptid
)
183 struct threadlist_entry
*thread
;
185 thread_object
*found
= NULL
;
191 inf_obj
= find_inferior_object (pid
);
196 for (thread
= ((inferior_object
*)inf_obj
)->threads
; thread
;
197 thread
= thread
->next
)
198 if (ptid_equal (thread
->thread_obj
->thread
->ptid
, ptid
))
200 found
= thread
->thread_obj
;
213 add_thread_object (struct thread_info
*tp
)
215 struct cleanup
*cleanup
;
216 thread_object
*thread_obj
;
217 inferior_object
*inf_obj
;
218 struct threadlist_entry
*entry
;
220 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
222 thread_obj
= create_thread_object (tp
);
225 gdbpy_print_stack ();
226 do_cleanups (cleanup
);
230 inf_obj
= (inferior_object
*) thread_obj
->inf_obj
;
232 entry
= xmalloc (sizeof (struct threadlist_entry
));
233 entry
->thread_obj
= thread_obj
;
234 entry
->next
= inf_obj
->threads
;
236 inf_obj
->threads
= entry
;
239 do_cleanups (cleanup
);
243 delete_thread_object (struct thread_info
*tp
, int ignore
)
245 struct cleanup
*cleanup
;
246 inferior_object
*inf_obj
;
247 thread_object
*thread_obj
;
248 struct threadlist_entry
**entry
, *tmp
;
250 inf_obj
= (inferior_object
*) find_inferior_object (PIDGET(tp
->ptid
));
254 /* Find thread entry in its inferior's thread_list. */
255 for (entry
= &inf_obj
->threads
; *entry
!= NULL
; entry
=
257 if ((*entry
)->thread_obj
->thread
== tp
)
266 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
269 tmp
->thread_obj
->thread
= NULL
;
271 *entry
= (*entry
)->next
;
274 Py_DECREF (tmp
->thread_obj
);
278 do_cleanups (cleanup
);
282 infpy_threads (PyObject
*self
, PyObject
*args
)
285 struct threadlist_entry
*entry
;
286 inferior_object
*inf_obj
= (inferior_object
*) self
;
289 INFPY_REQUIRE_VALID (inf_obj
);
291 tuple
= PyTuple_New (inf_obj
->nthreads
);
295 for (i
= 0, entry
= inf_obj
->threads
; i
< inf_obj
->nthreads
;
296 i
++, entry
= entry
->next
)
298 Py_INCREF (entry
->thread_obj
);
299 PyTuple_SET_ITEM (tuple
, i
, (PyObject
*) entry
->thread_obj
);
306 infpy_get_num (PyObject
*self
, void *closure
)
308 inferior_object
*inf
= (inferior_object
*) self
;
310 INFPY_REQUIRE_VALID (inf
);
312 return PyLong_FromLong (inf
->inferior
->num
);
316 infpy_get_pid (PyObject
*self
, void *closure
)
318 inferior_object
*inf
= (inferior_object
*) self
;
320 INFPY_REQUIRE_VALID (inf
);
322 return PyLong_FromLong (inf
->inferior
->pid
);
326 infpy_get_was_attached (PyObject
*self
, void *closure
)
328 inferior_object
*inf
= (inferior_object
*) self
;
330 INFPY_REQUIRE_VALID (inf
);
331 if (inf
->inferior
->attach_flag
)
337 build_inferior_list (struct inferior
*inf
, void *arg
)
339 PyObject
*list
= arg
;
340 PyObject
*inferior
= inferior_to_inferior_object (inf
);
346 success
= PyList_Append (list
, inferior
);
347 Py_DECREF (inferior
);
355 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
356 Returns a tuple of all inferiors. */
358 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
361 PyObject
*list
, *inferior
;
362 struct inferior
*inf
;
364 list
= PyList_New (0);
368 if (iterate_over_inferiors (build_inferior_list
, list
))
374 return PyList_AsTuple (list
);
377 /* Membuf and memory manipulation. */
379 /* Implementation of gdb.read_memory (address, length).
380 Returns a Python buffer object with LENGTH bytes of the inferior's
381 memory at ADDRESS. Both arguments are integers. Returns NULL on error,
382 with a python exception set. */
384 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
387 CORE_ADDR addr
, length
;
389 membuf_object
*membuf_obj
;
390 PyObject
*addr_obj
, *length_obj
;
391 struct cleanup
*cleanups
;
392 volatile struct gdb_exception except
;
393 static char *keywords
[] = { "address", "length", NULL
};
395 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
396 &addr_obj
, &length_obj
))
399 cleanups
= make_cleanup (null_cleanup
, NULL
);
401 TRY_CATCH (except
, RETURN_MASK_ALL
)
403 if (!get_addr_from_python (addr_obj
, &addr
)
404 || !get_addr_from_python (length_obj
, &length
))
410 buffer
= xmalloc (length
);
411 make_cleanup (xfree
, buffer
);
413 read_memory (addr
, buffer
, length
);
415 if (except
.reason
< 0)
417 do_cleanups (cleanups
);
418 GDB_PY_HANDLE_EXCEPTION (except
);
423 do_cleanups (cleanups
);
427 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
428 if (membuf_obj
== NULL
)
430 PyErr_SetString (PyExc_MemoryError
,
431 _("Could not allocate memory buffer object."));
432 do_cleanups (cleanups
);
436 discard_cleanups (cleanups
);
438 membuf_obj
->buffer
= buffer
;
439 membuf_obj
->addr
= addr
;
440 membuf_obj
->length
= length
;
442 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
446 /* Implementation of gdb.write_memory (address, buffer [, length]).
447 Writes the contents of BUFFER (a Python object supporting the read
448 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
449 bytes from BUFFER, or its entire contents if the argument is not
450 provided. The function returns nothing. Returns NULL on error, with
451 a python exception set. */
453 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
458 CORE_ADDR addr
, length
;
459 PyObject
*addr_obj
, *length_obj
= NULL
;
460 volatile struct gdb_exception except
;
461 static char *keywords
[] = { "address", "buffer", "length", NULL
};
464 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
465 &addr_obj
, &buffer
, &buf_len
,
469 TRY_CATCH (except
, RETURN_MASK_ALL
)
471 if (!get_addr_from_python (addr_obj
, &addr
))
479 else if (!get_addr_from_python (length_obj
, &length
))
484 write_memory (addr
, buffer
, length
);
486 GDB_PY_HANDLE_EXCEPTION (except
);
494 /* Destructor of Membuf objects. */
496 mbpy_dealloc (PyObject
*self
)
498 xfree (((membuf_object
*) self
)->buffer
);
499 self
->ob_type
->tp_free (self
);
502 /* Return a description of the Membuf object. */
504 mbpy_str (PyObject
*self
)
506 membuf_object
*membuf_obj
= (membuf_object
*) self
;
508 return PyString_FromFormat (_("Memory buffer for address %s, \
509 which is %s bytes long."),
510 paddress (python_gdbarch
, membuf_obj
->addr
),
511 pulongest (membuf_obj
->length
));
515 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
517 membuf_object
*membuf_obj
= (membuf_object
*) self
;
521 PyErr_SetString (PyExc_SystemError
,
522 _("The memory buffer supports only one segment."));
526 *ptrptr
= membuf_obj
->buffer
;
528 return membuf_obj
->length
;
532 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
534 return get_read_buffer (self
, segment
, ptrptr
);
538 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
541 *lenp
= ((membuf_object
*) self
)->length
;
547 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
552 ret
= get_read_buffer (self
, segment
, &ptr
);
553 *ptrptr
= (char *) ptr
;
559 gdb.search_memory (address, length, pattern). ADDRESS is the
560 address to start the search. LENGTH specifies the scope of the
561 search from ADDRESS. PATTERN is the pattern to search for (and
562 must be a Python object supporting the buffer protocol).
563 Returns a Python Long object holding the address where the pattern
564 was located, or if the pattern was not found, returns None. Returns NULL
565 on error, with a python exception set. */
567 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
569 CORE_ADDR start_addr
, length
;
570 static char *keywords
[] = { "address", "length", "pattern", NULL
};
571 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
572 volatile struct gdb_exception except
;
573 Py_ssize_t pattern_size
;
575 CORE_ADDR found_addr
;
578 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
579 &start_addr_obj
, &length_obj
,
583 if (get_addr_from_python (start_addr_obj
, &start_addr
)
584 && get_addr_from_python (length_obj
, &length
))
588 PyErr_SetString (PyExc_ValueError
,
589 _("Search range is empty."));
592 /* Watch for overflows. */
593 else if (length
> CORE_ADDR_MAX
594 || (start_addr
+ length
- 1) < start_addr
)
596 PyErr_SetString (PyExc_ValueError
,
597 _("The search range is too large."));
605 if (!PyObject_CheckReadBuffer (pattern
))
607 PyErr_SetString (PyExc_RuntimeError
,
608 _("The pattern is not a Python buffer."));
613 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
616 TRY_CATCH (except
, RETURN_MASK_ALL
)
618 found
= target_search_memory (start_addr
, length
,
619 buffer
, pattern_size
,
622 GDB_PY_HANDLE_EXCEPTION (except
);
625 return PyLong_FromLong (found_addr
);
630 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
631 Returns True if this inferior object still exists in GDB. */
634 infpy_is_valid (PyObject
*self
, PyObject
*args
)
636 inferior_object
*inf
= (inferior_object
*) self
;
645 infpy_dealloc (PyObject
*obj
)
647 inferior_object
*inf_obj
= (inferior_object
*) obj
;
648 struct inferior
*inf
= inf_obj
->inferior
;
653 set_inferior_data (inf
, infpy_inf_data_key
, NULL
);
656 /* Clear the INFERIOR pointer in an Inferior object and clear the
659 py_free_inferior (struct inferior
*inf
, void *datum
)
662 struct cleanup
*cleanup
;
663 inferior_object
*inf_obj
= datum
;
664 struct threadlist_entry
*th_entry
, *th_tmp
;
666 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
668 inf_obj
->inferior
= NULL
;
670 /* Deallocate threads list. */
671 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
673 Py_DECREF (th_entry
->thread_obj
);
676 th_entry
= th_entry
->next
;
680 inf_obj
->nthreads
= 0;
682 Py_DECREF ((PyObject
*) inf_obj
);
683 do_cleanups (cleanup
);
687 gdbpy_initialize_inferior (void)
689 if (PyType_Ready (&inferior_object_type
) < 0)
692 Py_INCREF (&inferior_object_type
);
693 PyModule_AddObject (gdb_module
, "Inferior",
694 (PyObject
*) &inferior_object_type
);
697 register_inferior_data_with_cleanup (py_free_inferior
);
699 observer_attach_new_thread (add_thread_object
);
700 observer_attach_thread_exit (delete_thread_object
);
701 observer_attach_normal_stop (python_on_normal_stop
);
702 observer_attach_target_resumed (python_on_resume
);
703 observer_attach_inferior_exit (python_inferior_exit
);
705 if (PyType_Ready (&membuf_object_type
) < 0)
708 Py_INCREF (&membuf_object_type
);
709 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
710 &membuf_object_type
);
713 static PyGetSetDef inferior_object_getset
[] =
715 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
716 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
718 { "was_attached", infpy_get_was_attached
, NULL
,
719 "True if the inferior was created using 'attach'.", NULL
},
723 static PyMethodDef inferior_object_methods
[] =
725 { "is_valid", infpy_is_valid
, METH_NOARGS
,
726 "is_valid () -> Boolean.\n\
727 Return true if this inferior is valid, false if not." },
728 { "threads", infpy_threads
, METH_NOARGS
,
729 "Return all the threads of this inferior." },
730 { "read_memory", (PyCFunction
) infpy_read_memory
,
731 METH_VARARGS
| METH_KEYWORDS
,
732 "read_memory (address, length) -> buffer\n\
733 Return a buffer object for reading from the inferior's memory." },
734 { "write_memory", (PyCFunction
) infpy_write_memory
,
735 METH_VARARGS
| METH_KEYWORDS
,
736 "write_memory (address, buffer [, length])\n\
737 Write the given buffer object to the inferior's memory." },
738 { "search_memory", (PyCFunction
) infpy_search_memory
,
739 METH_VARARGS
| METH_KEYWORDS
,
740 "search_memory (address, length, pattern) -> long\n\
741 Return a long with the address of a match, or None." },
745 static PyTypeObject inferior_object_type
=
747 PyObject_HEAD_INIT (NULL
)
749 "gdb.Inferior", /* tp_name */
750 sizeof (inferior_object
), /* tp_basicsize */
752 infpy_dealloc
, /* tp_dealloc */
758 0, /* tp_as_number */
759 0, /* tp_as_sequence */
760 0, /* tp_as_mapping */
766 0, /* tp_as_buffer */
767 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
768 "GDB inferior object", /* tp_doc */
771 0, /* tp_richcompare */
772 0, /* tp_weaklistoffset */
775 inferior_object_methods
, /* tp_methods */
777 inferior_object_getset
, /* tp_getset */
780 0, /* tp_descr_get */
781 0, /* tp_descr_set */
782 0, /* tp_dictoffset */
787 /* Python doesn't provide a decent way to get compatibility here. */
788 #if HAVE_LIBPYTHON2_4
789 #define CHARBUFFERPROC_NAME getcharbufferproc
791 #define CHARBUFFERPROC_NAME charbufferproc
794 static PyBufferProcs buffer_procs
= {
798 /* The cast here works around a difference between Python 2.4 and
800 (CHARBUFFERPROC_NAME
) get_char_buffer
803 static PyTypeObject membuf_object_type
= {
804 PyObject_HEAD_INIT (NULL
)
806 "gdb.Membuf", /*tp_name*/
807 sizeof (membuf_object
), /*tp_basicsize*/
809 mbpy_dealloc
, /*tp_dealloc*/
816 0, /*tp_as_sequence*/
823 &buffer_procs
, /*tp_as_buffer*/
824 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
825 "GDB memory buffer object", /*tp_doc*/
828 0, /* tp_richcompare */
829 0, /* tp_weaklistoffset */
837 0, /* tp_descr_get */
838 0, /* tp_descr_set */
839 0, /* tp_dictoffset */
842 PyType_GenericNew
/* tp_new */