1 /* Python interface to inferiors.
3 Copyright (C) 2009, 2010 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"
30 struct threadlist_entry
{
31 thread_object
*thread_obj
;
32 struct threadlist_entry
*next
;
39 /* The inferior we represent. */
40 struct inferior
*inferior
;
42 /* thread_object instances under this inferior. This list owns a
43 reference to each object it contains. */
44 struct threadlist_entry
*threads
;
46 /* Number of threads in the list. */
50 static PyTypeObject inferior_object_type
;
52 static const struct inferior_data
*infpy_inf_data_key
;
58 /* These are kept just for mbpy_str. */
63 static PyTypeObject membuf_object_type
;
65 /* Require that INFERIOR be a valid inferior ID. */
66 #define INFPY_REQUIRE_VALID(Inferior) \
68 if (!Inferior->inferior) \
70 PyErr_SetString (PyExc_RuntimeError, \
71 _("Inferior no longer exists.")); \
76 /* Return a borrowed reference to the Python object of type Inferior
77 representing INFERIOR. If the object has already been created,
78 return it, otherwise, create it. Return NULL on failure. */
80 inferior_to_inferior_object (struct inferior
*inferior
)
82 inferior_object
*inf_obj
;
84 inf_obj
= inferior_data (inferior
, infpy_inf_data_key
);
87 struct cleanup
*cleanup
;
88 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
90 inf_obj
= PyObject_New (inferior_object
, &inferior_object_type
);
93 do_cleanups (cleanup
);
97 inf_obj
->inferior
= inferior
;
98 inf_obj
->threads
= NULL
;
99 inf_obj
->nthreads
= 0;
101 set_inferior_data (inferior
, infpy_inf_data_key
, inf_obj
);
103 do_cleanups (cleanup
);
106 return (PyObject
*) inf_obj
;
109 /* Finds the Python Inferior object for the given PID. Returns a
110 borrowed reference, or NULL if PID does not match any inferior
114 find_inferior_object (int pid
)
116 struct inflist_entry
*p
;
117 struct inferior
*inf
= find_inferior_pid (pid
);
120 return inferior_to_inferior_object (inf
);
126 find_thread_object (ptid_t ptid
)
129 struct threadlist_entry
*thread
;
133 inf_obj
= find_inferior_object (pid
);
136 for (thread
= ((inferior_object
*)inf_obj
)->threads
; thread
;
137 thread
= thread
->next
)
138 if (ptid_equal (thread
->thread_obj
->thread
->ptid
, ptid
))
139 return thread
->thread_obj
;
145 add_thread_object (struct thread_info
*tp
)
147 struct cleanup
*cleanup
;
148 thread_object
*thread_obj
;
149 inferior_object
*inf_obj
;
150 struct threadlist_entry
*entry
;
152 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
154 thread_obj
= create_thread_object (tp
);
157 gdbpy_print_stack ();
158 do_cleanups (cleanup
);
162 inf_obj
= (inferior_object
*) thread_obj
->inf_obj
;
164 entry
= xmalloc (sizeof (struct threadlist_entry
));
165 entry
->thread_obj
= thread_obj
;
166 entry
->next
= inf_obj
->threads
;
168 inf_obj
->threads
= entry
;
171 do_cleanups (cleanup
);
175 delete_thread_object (struct thread_info
*tp
, int ignore
)
177 struct cleanup
*cleanup
;
178 inferior_object
*inf_obj
;
179 thread_object
*thread_obj
;
180 struct threadlist_entry
**entry
, *tmp
;
182 inf_obj
= (inferior_object
*) find_inferior_object (PIDGET(tp
->ptid
));
186 /* Find thread entry in its inferior's thread_list. */
187 for (entry
= &inf_obj
->threads
; *entry
!= NULL
; entry
=
189 if ((*entry
)->thread_obj
->thread
== tp
)
195 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
198 tmp
->thread_obj
->thread
= NULL
;
200 *entry
= (*entry
)->next
;
203 Py_DECREF (tmp
->thread_obj
);
206 do_cleanups (cleanup
);
210 infpy_threads (PyObject
*self
, PyObject
*args
)
213 struct threadlist_entry
*entry
;
214 inferior_object
*inf_obj
= (inferior_object
*) self
;
217 INFPY_REQUIRE_VALID (inf_obj
);
219 tuple
= PyTuple_New (inf_obj
->nthreads
);
223 for (i
= 0, entry
= inf_obj
->threads
; i
< inf_obj
->nthreads
;
224 i
++, entry
= entry
->next
)
226 Py_INCREF (entry
->thread_obj
);
227 PyTuple_SET_ITEM (tuple
, i
, (PyObject
*) entry
->thread_obj
);
234 infpy_get_num (PyObject
*self
, void *closure
)
236 inferior_object
*inf
= (inferior_object
*) self
;
238 INFPY_REQUIRE_VALID (inf
);
240 return PyLong_FromLong (inf
->inferior
->num
);
244 infpy_get_pid (PyObject
*self
, void *closure
)
246 inferior_object
*inf
= (inferior_object
*) self
;
248 INFPY_REQUIRE_VALID (inf
);
250 return PyLong_FromLong (inf
->inferior
->pid
);
254 infpy_get_was_attached (PyObject
*self
, void *closure
)
256 inferior_object
*inf
= (inferior_object
*) self
;
258 INFPY_REQUIRE_VALID (inf
);
259 if (inf
->inferior
->attach_flag
)
265 build_inferior_list (struct inferior
*inf
, void *arg
)
267 PyObject
*list
= arg
;
268 PyObject
*inferior
= inferior_to_inferior_object (inf
);
270 PyList_Append (list
, inferior
);
274 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
275 Returns a tuple of all inferiors. */
277 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
280 PyObject
*list
, *inferior
;
281 struct inferior
*inf
;
283 list
= PyList_New (0);
287 iterate_over_inferiors (build_inferior_list
, list
);
289 return PyList_AsTuple (list
);
292 /* Membuf and memory manipulation. */
294 /* Implementation of gdb.read_memory (address, length).
295 Returns a Python buffer object with LENGTH bytes of the inferior's
296 memory at ADDRESS. Both arguments are integers. */
298 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
301 CORE_ADDR addr
, length
;
303 membuf_object
*membuf_obj
;
304 PyObject
*addr_obj
, *length_obj
;
305 struct cleanup
*cleanups
;
306 volatile struct gdb_exception except
;
307 static char *keywords
[] = { "address", "length", NULL
};
309 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
310 &addr_obj
, &length_obj
))
313 cleanups
= make_cleanup (null_cleanup
, NULL
);
315 TRY_CATCH (except
, RETURN_MASK_ALL
)
317 if (!get_addr_from_python (addr_obj
, &addr
)
318 || !get_addr_from_python (length_obj
, &length
))
324 buffer
= xmalloc (length
);
325 make_cleanup (xfree
, buffer
);
327 read_memory (addr
, buffer
, length
);
329 if (except
.reason
< 0)
331 do_cleanups (cleanups
);
332 GDB_PY_HANDLE_EXCEPTION (except
);
337 do_cleanups (cleanups
);
341 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
342 if (membuf_obj
== NULL
)
344 PyErr_SetString (PyExc_MemoryError
,
345 _("Could not allocate memory buffer object."));
346 do_cleanups (cleanups
);
350 discard_cleanups (cleanups
);
352 membuf_obj
->buffer
= buffer
;
353 membuf_obj
->addr
= addr
;
354 membuf_obj
->length
= length
;
356 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
360 /* Implementation of gdb.write_memory (address, buffer [, length]).
361 Writes the contents of BUFFER (a Python object supporting the read
362 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
363 bytes from BUFFER, or its entire contents if the argument is not
364 provided. The function returns nothing. */
366 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
368 int buf_len
, error
= 0;
370 CORE_ADDR addr
, length
;
371 PyObject
*addr_obj
, *length_obj
= NULL
;
372 volatile struct gdb_exception except
;
373 static char *keywords
[] = { "address", "buffer", "length", NULL
};
376 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
377 &addr_obj
, &buffer
, &buf_len
,
381 TRY_CATCH (except
, RETURN_MASK_ALL
)
383 if (!get_addr_from_python (addr_obj
, &addr
))
391 else if (!get_addr_from_python (length_obj
, &length
))
396 write_memory (addr
, buffer
, length
);
398 GDB_PY_HANDLE_EXCEPTION (except
);
406 /* Destructor of Membuf objects. */
408 mbpy_dealloc (PyObject
*self
)
410 xfree (((membuf_object
*) self
)->buffer
);
411 self
->ob_type
->tp_free (self
);
414 /* Return a description of the Membuf object. */
416 mbpy_str (PyObject
*self
)
418 membuf_object
*membuf_obj
= (membuf_object
*) self
;
420 return PyString_FromFormat (_("Memory buffer for address %s, \
421 which is %s bytes long."),
422 paddress (python_gdbarch
, membuf_obj
->addr
),
423 pulongest (membuf_obj
->length
));
427 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
429 membuf_object
*membuf_obj
= (membuf_object
*) self
;
433 PyErr_SetString (PyExc_SystemError
,
434 _("The memory buffer supports only one segment."));
438 *ptrptr
= membuf_obj
->buffer
;
440 return membuf_obj
->length
;
444 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
446 return get_read_buffer (self
, segment
, ptrptr
);
450 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
453 *lenp
= ((membuf_object
*) self
)->length
;
459 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
464 ret
= get_read_buffer (self
, segment
, &ptr
);
465 *ptrptr
= (char *) ptr
;
471 gdb.search_memory (address, length, pattern). ADDRESS is the
472 address to start the search. LENGTH specifies the scope of the
473 search from ADDRESS. PATTERN is the pattern to search for (and
474 must be a Python object supporting the buffer protocol).
475 Returns a Python Long object holding the address where the pattern
476 was located, or if the pattern was not found, returns None. */
478 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
480 CORE_ADDR start_addr
, length
;
481 static char *keywords
[] = { "address", "length", "pattern", NULL
};
482 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
483 volatile struct gdb_exception except
;
484 Py_ssize_t pattern_size
;
486 CORE_ADDR found_addr
;
489 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
490 &start_addr_obj
, &length_obj
,
494 if (get_addr_from_python (start_addr_obj
, &start_addr
)
495 && get_addr_from_python (length_obj
, &length
))
499 PyErr_SetString (PyExc_ValueError
,
500 _("Search range is empty."));
503 /* Watch for overflows. */
504 else if (length
> CORE_ADDR_MAX
505 || (start_addr
+ length
- 1) < start_addr
)
507 PyErr_SetString (PyExc_ValueError
,
508 _("The search range is too large."));
515 PyErr_SetString (PyExc_RuntimeError
,
516 _("Cannot get search address/range from Python."));
521 if (!PyObject_CheckReadBuffer (pattern
))
523 PyErr_SetString (PyExc_RuntimeError
,
524 _("The pattern is not a Python buffer."));
529 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
532 TRY_CATCH (except
, RETURN_MASK_ALL
)
534 found
= target_search_memory (start_addr
, length
,
535 buffer
, pattern_size
,
538 GDB_PY_HANDLE_EXCEPTION (except
);
541 return PyLong_FromLong (found_addr
);
547 /* Clear the INFERIOR pointer in an Inferior object and clear the
550 py_free_inferior (struct inferior
*inf
, void *datum
)
553 struct cleanup
*cleanup
;
554 inferior_object
*inf_obj
= datum
;
555 struct threadlist_entry
*th_entry
, *th_tmp
;
557 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
559 inf_obj
->inferior
= NULL
;
561 /* Deallocate threads list. */
562 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
564 Py_DECREF (th_entry
->thread_obj
);
567 th_entry
= th_entry
->next
;
571 inf_obj
->nthreads
= 0;
573 Py_DECREF ((PyObject
*) inf_obj
);
574 do_cleanups (cleanup
);
578 gdbpy_initialize_inferior (void)
580 if (PyType_Ready (&inferior_object_type
) < 0)
583 Py_INCREF (&inferior_object_type
);
584 PyModule_AddObject (gdb_module
, "Inferior",
585 (PyObject
*) &inferior_object_type
);
588 register_inferior_data_with_cleanup (py_free_inferior
);
590 observer_attach_new_thread (add_thread_object
);
591 observer_attach_thread_exit (delete_thread_object
);
593 if (PyType_Ready (&membuf_object_type
) < 0)
596 Py_INCREF (&membuf_object_type
);
597 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
598 &membuf_object_type
);
601 static PyGetSetDef inferior_object_getset
[] =
603 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
604 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
606 { "was_attached", infpy_get_was_attached
, NULL
,
607 "True if the inferior was created using 'attach'.", NULL
},
611 static PyMethodDef inferior_object_methods
[] =
613 { "threads", infpy_threads
, METH_NOARGS
,
614 "Return all the threads of this inferior." },
615 { "read_memory", (PyCFunction
) infpy_read_memory
,
616 METH_VARARGS
| METH_KEYWORDS
,
617 "read_memory (address, length) -> buffer\n\
618 Return a buffer object for reading from the inferior's memory." },
619 { "write_memory", (PyCFunction
) infpy_write_memory
,
620 METH_VARARGS
| METH_KEYWORDS
,
621 "write_memory (address, buffer [, length])\n\
622 Write the given buffer object to the inferior's memory." },
623 { "search_memory", (PyCFunction
) infpy_search_memory
,
624 METH_VARARGS
| METH_KEYWORDS
,
625 "search_memory (address, length, pattern) -> long\n\
626 Return a long with the address of a match, or None." },
630 static PyTypeObject inferior_object_type
=
632 PyObject_HEAD_INIT (NULL
)
634 "gdb.Inferior", /* tp_name */
635 sizeof (inferior_object
), /* tp_basicsize */
643 0, /* tp_as_number */
644 0, /* tp_as_sequence */
645 0, /* tp_as_mapping */
651 0, /* tp_as_buffer */
652 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
653 "GDB inferior object", /* tp_doc */
656 0, /* tp_richcompare */
657 0, /* tp_weaklistoffset */
660 inferior_object_methods
, /* tp_methods */
662 inferior_object_getset
, /* tp_getset */
665 0, /* tp_descr_get */
666 0, /* tp_descr_set */
667 0, /* tp_dictoffset */
672 /* Python doesn't provide a decent way to get compatibility here. */
673 #if HAVE_LIBPYTHON2_4
674 #define CHARBUFFERPROC_NAME getcharbufferproc
676 #define CHARBUFFERPROC_NAME charbufferproc
679 static PyBufferProcs buffer_procs
= {
683 /* The cast here works around a difference between Python 2.4 and
685 (CHARBUFFERPROC_NAME
) get_char_buffer
688 static PyTypeObject membuf_object_type
= {
689 PyObject_HEAD_INIT (NULL
)
691 "gdb.Membuf", /*tp_name*/
692 sizeof (membuf_object
), /*tp_basicsize*/
694 mbpy_dealloc
, /*tp_dealloc*/
701 0, /*tp_as_sequence*/
708 &buffer_procs
, /*tp_as_buffer*/
709 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
710 "GDB memory buffer object", /*tp_doc*/
713 0, /* tp_richcompare */
714 0, /* tp_weaklistoffset */
722 0, /* tp_descr_get */
723 0, /* tp_descr_set */
724 0, /* tp_dictoffset */
727 PyType_GenericNew
/* tp_new */