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 PyList_Append (list
, inferior
);
333 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
334 Returns a tuple of all inferiors. */
336 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
339 PyObject
*list
, *inferior
;
340 struct inferior
*inf
;
342 list
= PyList_New (0);
346 iterate_over_inferiors (build_inferior_list
, list
);
348 return PyList_AsTuple (list
);
351 /* Membuf and memory manipulation. */
353 /* Implementation of gdb.read_memory (address, length).
354 Returns a Python buffer object with LENGTH bytes of the inferior's
355 memory at ADDRESS. Both arguments are integers. Returns NULL on error,
356 with a python exception set. */
358 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
361 CORE_ADDR addr
, length
;
363 membuf_object
*membuf_obj
;
364 PyObject
*addr_obj
, *length_obj
;
365 struct cleanup
*cleanups
;
366 volatile struct gdb_exception except
;
367 static char *keywords
[] = { "address", "length", NULL
};
369 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
370 &addr_obj
, &length_obj
))
373 cleanups
= make_cleanup (null_cleanup
, NULL
);
375 TRY_CATCH (except
, RETURN_MASK_ALL
)
377 if (!get_addr_from_python (addr_obj
, &addr
)
378 || !get_addr_from_python (length_obj
, &length
))
384 buffer
= xmalloc (length
);
385 make_cleanup (xfree
, buffer
);
387 read_memory (addr
, buffer
, length
);
389 if (except
.reason
< 0)
391 do_cleanups (cleanups
);
392 GDB_PY_HANDLE_EXCEPTION (except
);
397 do_cleanups (cleanups
);
401 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
402 if (membuf_obj
== NULL
)
404 PyErr_SetString (PyExc_MemoryError
,
405 _("Could not allocate memory buffer object."));
406 do_cleanups (cleanups
);
410 discard_cleanups (cleanups
);
412 membuf_obj
->buffer
= buffer
;
413 membuf_obj
->addr
= addr
;
414 membuf_obj
->length
= length
;
416 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
420 /* Implementation of gdb.write_memory (address, buffer [, length]).
421 Writes the contents of BUFFER (a Python object supporting the read
422 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
423 bytes from BUFFER, or its entire contents if the argument is not
424 provided. The function returns nothing. Returns NULL on error, with
425 a python exception set. */
427 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
429 int buf_len
, error
= 0;
431 CORE_ADDR addr
, length
;
432 PyObject
*addr_obj
, *length_obj
= NULL
;
433 volatile struct gdb_exception except
;
434 static char *keywords
[] = { "address", "buffer", "length", NULL
};
437 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
438 &addr_obj
, &buffer
, &buf_len
,
442 TRY_CATCH (except
, RETURN_MASK_ALL
)
444 if (!get_addr_from_python (addr_obj
, &addr
))
452 else if (!get_addr_from_python (length_obj
, &length
))
457 write_memory (addr
, buffer
, length
);
459 GDB_PY_HANDLE_EXCEPTION (except
);
467 /* Destructor of Membuf objects. */
469 mbpy_dealloc (PyObject
*self
)
471 xfree (((membuf_object
*) self
)->buffer
);
472 self
->ob_type
->tp_free (self
);
475 /* Return a description of the Membuf object. */
477 mbpy_str (PyObject
*self
)
479 membuf_object
*membuf_obj
= (membuf_object
*) self
;
481 return PyString_FromFormat (_("Memory buffer for address %s, \
482 which is %s bytes long."),
483 paddress (python_gdbarch
, membuf_obj
->addr
),
484 pulongest (membuf_obj
->length
));
488 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
490 membuf_object
*membuf_obj
= (membuf_object
*) self
;
494 PyErr_SetString (PyExc_SystemError
,
495 _("The memory buffer supports only one segment."));
499 *ptrptr
= membuf_obj
->buffer
;
501 return membuf_obj
->length
;
505 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
507 return get_read_buffer (self
, segment
, ptrptr
);
511 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
514 *lenp
= ((membuf_object
*) self
)->length
;
520 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
525 ret
= get_read_buffer (self
, segment
, &ptr
);
526 *ptrptr
= (char *) ptr
;
532 gdb.search_memory (address, length, pattern). ADDRESS is the
533 address to start the search. LENGTH specifies the scope of the
534 search from ADDRESS. PATTERN is the pattern to search for (and
535 must be a Python object supporting the buffer protocol).
536 Returns a Python Long object holding the address where the pattern
537 was located, or if the pattern was not found, returns None. Returns NULL
538 on error, with a python exception set. */
540 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
542 CORE_ADDR start_addr
, length
;
543 static char *keywords
[] = { "address", "length", "pattern", NULL
};
544 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
545 volatile struct gdb_exception except
;
546 Py_ssize_t pattern_size
;
548 CORE_ADDR found_addr
;
551 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
552 &start_addr_obj
, &length_obj
,
556 if (get_addr_from_python (start_addr_obj
, &start_addr
)
557 && get_addr_from_python (length_obj
, &length
))
561 PyErr_SetString (PyExc_ValueError
,
562 _("Search range is empty."));
565 /* Watch for overflows. */
566 else if (length
> CORE_ADDR_MAX
567 || (start_addr
+ length
- 1) < start_addr
)
569 PyErr_SetString (PyExc_ValueError
,
570 _("The search range is too large."));
578 if (!PyObject_CheckReadBuffer (pattern
))
580 PyErr_SetString (PyExc_RuntimeError
,
581 _("The pattern is not a Python buffer."));
586 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
589 TRY_CATCH (except
, RETURN_MASK_ALL
)
591 found
= target_search_memory (start_addr
, length
,
592 buffer
, pattern_size
,
595 GDB_PY_HANDLE_EXCEPTION (except
);
598 return PyLong_FromLong (found_addr
);
604 /* Clear the INFERIOR pointer in an Inferior object and clear the
607 py_free_inferior (struct inferior
*inf
, void *datum
)
610 struct cleanup
*cleanup
;
611 inferior_object
*inf_obj
= datum
;
612 struct threadlist_entry
*th_entry
, *th_tmp
;
614 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
616 inf_obj
->inferior
= NULL
;
618 /* Deallocate threads list. */
619 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
621 Py_DECREF (th_entry
->thread_obj
);
624 th_entry
= th_entry
->next
;
628 inf_obj
->nthreads
= 0;
630 Py_DECREF ((PyObject
*) inf_obj
);
631 do_cleanups (cleanup
);
635 gdbpy_initialize_inferior (void)
637 if (PyType_Ready (&inferior_object_type
) < 0)
640 Py_INCREF (&inferior_object_type
);
641 PyModule_AddObject (gdb_module
, "Inferior",
642 (PyObject
*) &inferior_object_type
);
645 register_inferior_data_with_cleanup (py_free_inferior
);
647 observer_attach_new_thread (add_thread_object
);
648 observer_attach_thread_exit (delete_thread_object
);
649 observer_attach_normal_stop (python_on_normal_stop
);
650 observer_attach_target_resumed (python_on_resume
);
651 observer_attach_inferior_exit (python_inferior_exit
);
653 if (PyType_Ready (&membuf_object_type
) < 0)
656 Py_INCREF (&membuf_object_type
);
657 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
658 &membuf_object_type
);
661 static PyGetSetDef inferior_object_getset
[] =
663 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
664 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
666 { "was_attached", infpy_get_was_attached
, NULL
,
667 "True if the inferior was created using 'attach'.", NULL
},
671 static PyMethodDef inferior_object_methods
[] =
673 { "threads", infpy_threads
, METH_NOARGS
,
674 "Return all the threads of this inferior." },
675 { "read_memory", (PyCFunction
) infpy_read_memory
,
676 METH_VARARGS
| METH_KEYWORDS
,
677 "read_memory (address, length) -> buffer\n\
678 Return a buffer object for reading from the inferior's memory." },
679 { "write_memory", (PyCFunction
) infpy_write_memory
,
680 METH_VARARGS
| METH_KEYWORDS
,
681 "write_memory (address, buffer [, length])\n\
682 Write the given buffer object to the inferior's memory." },
683 { "search_memory", (PyCFunction
) infpy_search_memory
,
684 METH_VARARGS
| METH_KEYWORDS
,
685 "search_memory (address, length, pattern) -> long\n\
686 Return a long with the address of a match, or None." },
690 static PyTypeObject inferior_object_type
=
692 PyObject_HEAD_INIT (NULL
)
694 "gdb.Inferior", /* tp_name */
695 sizeof (inferior_object
), /* tp_basicsize */
703 0, /* tp_as_number */
704 0, /* tp_as_sequence */
705 0, /* tp_as_mapping */
711 0, /* tp_as_buffer */
712 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
713 "GDB inferior object", /* tp_doc */
716 0, /* tp_richcompare */
717 0, /* tp_weaklistoffset */
720 inferior_object_methods
, /* tp_methods */
722 inferior_object_getset
, /* tp_getset */
725 0, /* tp_descr_get */
726 0, /* tp_descr_set */
727 0, /* tp_dictoffset */
732 /* Python doesn't provide a decent way to get compatibility here. */
733 #if HAVE_LIBPYTHON2_4
734 #define CHARBUFFERPROC_NAME getcharbufferproc
736 #define CHARBUFFERPROC_NAME charbufferproc
739 static PyBufferProcs buffer_procs
= {
743 /* The cast here works around a difference between Python 2.4 and
745 (CHARBUFFERPROC_NAME
) get_char_buffer
748 static PyTypeObject membuf_object_type
= {
749 PyObject_HEAD_INIT (NULL
)
751 "gdb.Membuf", /*tp_name*/
752 sizeof (membuf_object
), /*tp_basicsize*/
754 mbpy_dealloc
, /*tp_dealloc*/
761 0, /*tp_as_sequence*/
768 &buffer_procs
, /*tp_as_buffer*/
769 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
770 "GDB memory buffer object", /*tp_doc*/
773 0, /* tp_richcompare */
774 0, /* tp_weaklistoffset */
782 0, /* tp_descr_get */
783 0, /* tp_descr_set */
784 0, /* tp_dictoffset */
787 PyType_GenericNew
/* tp_new */