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. Returns NULL on error,
297 with a python exception set. */
299 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
302 CORE_ADDR addr
, length
;
304 membuf_object
*membuf_obj
;
305 PyObject
*addr_obj
, *length_obj
;
306 struct cleanup
*cleanups
;
307 volatile struct gdb_exception except
;
308 static char *keywords
[] = { "address", "length", NULL
};
310 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
311 &addr_obj
, &length_obj
))
314 cleanups
= make_cleanup (null_cleanup
, NULL
);
316 TRY_CATCH (except
, RETURN_MASK_ALL
)
318 if (!get_addr_from_python (addr_obj
, &addr
)
319 || !get_addr_from_python (length_obj
, &length
))
325 buffer
= xmalloc (length
);
326 make_cleanup (xfree
, buffer
);
328 read_memory (addr
, buffer
, length
);
330 if (except
.reason
< 0)
332 do_cleanups (cleanups
);
333 GDB_PY_HANDLE_EXCEPTION (except
);
338 do_cleanups (cleanups
);
342 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
343 if (membuf_obj
== NULL
)
345 PyErr_SetString (PyExc_MemoryError
,
346 _("Could not allocate memory buffer object."));
347 do_cleanups (cleanups
);
351 discard_cleanups (cleanups
);
353 membuf_obj
->buffer
= buffer
;
354 membuf_obj
->addr
= addr
;
355 membuf_obj
->length
= length
;
357 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
361 /* Implementation of gdb.write_memory (address, buffer [, length]).
362 Writes the contents of BUFFER (a Python object supporting the read
363 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
364 bytes from BUFFER, or its entire contents if the argument is not
365 provided. The function returns nothing. Returns NULL on error, with
366 a python exception set. */
368 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
370 int buf_len
, error
= 0;
372 CORE_ADDR addr
, length
;
373 PyObject
*addr_obj
, *length_obj
= NULL
;
374 volatile struct gdb_exception except
;
375 static char *keywords
[] = { "address", "buffer", "length", NULL
};
378 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
379 &addr_obj
, &buffer
, &buf_len
,
383 TRY_CATCH (except
, RETURN_MASK_ALL
)
385 if (!get_addr_from_python (addr_obj
, &addr
))
393 else if (!get_addr_from_python (length_obj
, &length
))
398 write_memory (addr
, buffer
, length
);
400 GDB_PY_HANDLE_EXCEPTION (except
);
408 /* Destructor of Membuf objects. */
410 mbpy_dealloc (PyObject
*self
)
412 xfree (((membuf_object
*) self
)->buffer
);
413 self
->ob_type
->tp_free (self
);
416 /* Return a description of the Membuf object. */
418 mbpy_str (PyObject
*self
)
420 membuf_object
*membuf_obj
= (membuf_object
*) self
;
422 return PyString_FromFormat (_("Memory buffer for address %s, \
423 which is %s bytes long."),
424 paddress (python_gdbarch
, membuf_obj
->addr
),
425 pulongest (membuf_obj
->length
));
429 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
431 membuf_object
*membuf_obj
= (membuf_object
*) self
;
435 PyErr_SetString (PyExc_SystemError
,
436 _("The memory buffer supports only one segment."));
440 *ptrptr
= membuf_obj
->buffer
;
442 return membuf_obj
->length
;
446 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
448 return get_read_buffer (self
, segment
, ptrptr
);
452 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
455 *lenp
= ((membuf_object
*) self
)->length
;
461 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
466 ret
= get_read_buffer (self
, segment
, &ptr
);
467 *ptrptr
= (char *) ptr
;
473 gdb.search_memory (address, length, pattern). ADDRESS is the
474 address to start the search. LENGTH specifies the scope of the
475 search from ADDRESS. PATTERN is the pattern to search for (and
476 must be a Python object supporting the buffer protocol).
477 Returns a Python Long object holding the address where the pattern
478 was located, or if the pattern was not found, returns None. Returns NULL
479 on error, with a python exception set. */
481 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
483 CORE_ADDR start_addr
, length
;
484 static char *keywords
[] = { "address", "length", "pattern", NULL
};
485 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
486 volatile struct gdb_exception except
;
487 Py_ssize_t pattern_size
;
489 CORE_ADDR found_addr
;
492 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
493 &start_addr_obj
, &length_obj
,
497 if (get_addr_from_python (start_addr_obj
, &start_addr
)
498 && get_addr_from_python (length_obj
, &length
))
502 PyErr_SetString (PyExc_ValueError
,
503 _("Search range is empty."));
506 /* Watch for overflows. */
507 else if (length
> CORE_ADDR_MAX
508 || (start_addr
+ length
- 1) < start_addr
)
510 PyErr_SetString (PyExc_ValueError
,
511 _("The search range is too large."));
519 if (!PyObject_CheckReadBuffer (pattern
))
521 PyErr_SetString (PyExc_RuntimeError
,
522 _("The pattern is not a Python buffer."));
527 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
530 TRY_CATCH (except
, RETURN_MASK_ALL
)
532 found
= target_search_memory (start_addr
, length
,
533 buffer
, pattern_size
,
536 GDB_PY_HANDLE_EXCEPTION (except
);
539 return PyLong_FromLong (found_addr
);
545 /* Clear the INFERIOR pointer in an Inferior object and clear the
548 py_free_inferior (struct inferior
*inf
, void *datum
)
551 struct cleanup
*cleanup
;
552 inferior_object
*inf_obj
= datum
;
553 struct threadlist_entry
*th_entry
, *th_tmp
;
555 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
557 inf_obj
->inferior
= NULL
;
559 /* Deallocate threads list. */
560 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
562 Py_DECREF (th_entry
->thread_obj
);
565 th_entry
= th_entry
->next
;
569 inf_obj
->nthreads
= 0;
571 Py_DECREF ((PyObject
*) inf_obj
);
572 do_cleanups (cleanup
);
576 gdbpy_initialize_inferior (void)
578 if (PyType_Ready (&inferior_object_type
) < 0)
581 Py_INCREF (&inferior_object_type
);
582 PyModule_AddObject (gdb_module
, "Inferior",
583 (PyObject
*) &inferior_object_type
);
586 register_inferior_data_with_cleanup (py_free_inferior
);
588 observer_attach_new_thread (add_thread_object
);
589 observer_attach_thread_exit (delete_thread_object
);
591 if (PyType_Ready (&membuf_object_type
) < 0)
594 Py_INCREF (&membuf_object_type
);
595 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
596 &membuf_object_type
);
599 static PyGetSetDef inferior_object_getset
[] =
601 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
602 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
604 { "was_attached", infpy_get_was_attached
, NULL
,
605 "True if the inferior was created using 'attach'.", NULL
},
609 static PyMethodDef inferior_object_methods
[] =
611 { "threads", infpy_threads
, METH_NOARGS
,
612 "Return all the threads of this inferior." },
613 { "read_memory", (PyCFunction
) infpy_read_memory
,
614 METH_VARARGS
| METH_KEYWORDS
,
615 "read_memory (address, length) -> buffer\n\
616 Return a buffer object for reading from the inferior's memory." },
617 { "write_memory", (PyCFunction
) infpy_write_memory
,
618 METH_VARARGS
| METH_KEYWORDS
,
619 "write_memory (address, buffer [, length])\n\
620 Write the given buffer object to the inferior's memory." },
621 { "search_memory", (PyCFunction
) infpy_search_memory
,
622 METH_VARARGS
| METH_KEYWORDS
,
623 "search_memory (address, length, pattern) -> long\n\
624 Return a long with the address of a match, or None." },
628 static PyTypeObject inferior_object_type
=
630 PyObject_HEAD_INIT (NULL
)
632 "gdb.Inferior", /* tp_name */
633 sizeof (inferior_object
), /* tp_basicsize */
641 0, /* tp_as_number */
642 0, /* tp_as_sequence */
643 0, /* tp_as_mapping */
649 0, /* tp_as_buffer */
650 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
651 "GDB inferior object", /* tp_doc */
654 0, /* tp_richcompare */
655 0, /* tp_weaklistoffset */
658 inferior_object_methods
, /* tp_methods */
660 inferior_object_getset
, /* tp_getset */
663 0, /* tp_descr_get */
664 0, /* tp_descr_set */
665 0, /* tp_dictoffset */
670 /* Python doesn't provide a decent way to get compatibility here. */
671 #if HAVE_LIBPYTHON2_4
672 #define CHARBUFFERPROC_NAME getcharbufferproc
674 #define CHARBUFFERPROC_NAME charbufferproc
677 static PyBufferProcs buffer_procs
= {
681 /* The cast here works around a difference between Python 2.4 and
683 (CHARBUFFERPROC_NAME
) get_char_buffer
686 static PyTypeObject membuf_object_type
= {
687 PyObject_HEAD_INIT (NULL
)
689 "gdb.Membuf", /*tp_name*/
690 sizeof (membuf_object
), /*tp_basicsize*/
692 mbpy_dealloc
, /*tp_dealloc*/
699 0, /*tp_as_sequence*/
706 &buffer_procs
, /*tp_as_buffer*/
707 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
708 "GDB memory buffer object", /*tp_doc*/
711 0, /* tp_richcompare */
712 0, /* tp_weaklistoffset */
720 0, /* tp_descr_get */
721 0, /* tp_descr_set */
722 0, /* tp_dictoffset */
725 PyType_GenericNew
/* tp_new */