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"
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
;
136 inf_obj
= find_inferior_object (pid
);
139 for (thread
= ((inferior_object
*)inf_obj
)->threads
; thread
;
140 thread
= thread
->next
)
141 if (ptid_equal (thread
->thread_obj
->thread
->ptid
, ptid
))
142 return thread
->thread_obj
;
148 add_thread_object (struct thread_info
*tp
)
150 struct cleanup
*cleanup
;
151 thread_object
*thread_obj
;
152 inferior_object
*inf_obj
;
153 struct threadlist_entry
*entry
;
155 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
157 thread_obj
= create_thread_object (tp
);
160 gdbpy_print_stack ();
161 do_cleanups (cleanup
);
165 inf_obj
= (inferior_object
*) thread_obj
->inf_obj
;
167 entry
= xmalloc (sizeof (struct threadlist_entry
));
168 entry
->thread_obj
= thread_obj
;
169 entry
->next
= inf_obj
->threads
;
171 inf_obj
->threads
= entry
;
174 do_cleanups (cleanup
);
178 delete_thread_object (struct thread_info
*tp
, int ignore
)
180 struct cleanup
*cleanup
;
181 inferior_object
*inf_obj
;
182 thread_object
*thread_obj
;
183 struct threadlist_entry
**entry
, *tmp
;
185 inf_obj
= (inferior_object
*) find_inferior_object (PIDGET(tp
->ptid
));
189 /* Find thread entry in its inferior's thread_list. */
190 for (entry
= &inf_obj
->threads
; *entry
!= NULL
; entry
=
192 if ((*entry
)->thread_obj
->thread
== tp
)
198 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
201 tmp
->thread_obj
->thread
= NULL
;
203 *entry
= (*entry
)->next
;
206 Py_DECREF (tmp
->thread_obj
);
209 do_cleanups (cleanup
);
213 infpy_threads (PyObject
*self
, PyObject
*args
)
216 struct threadlist_entry
*entry
;
217 inferior_object
*inf_obj
= (inferior_object
*) self
;
220 INFPY_REQUIRE_VALID (inf_obj
);
222 tuple
= PyTuple_New (inf_obj
->nthreads
);
226 for (i
= 0, entry
= inf_obj
->threads
; i
< inf_obj
->nthreads
;
227 i
++, entry
= entry
->next
)
229 Py_INCREF (entry
->thread_obj
);
230 PyTuple_SET_ITEM (tuple
, i
, (PyObject
*) entry
->thread_obj
);
237 infpy_get_num (PyObject
*self
, void *closure
)
239 inferior_object
*inf
= (inferior_object
*) self
;
241 INFPY_REQUIRE_VALID (inf
);
243 return PyLong_FromLong (inf
->inferior
->num
);
247 infpy_get_pid (PyObject
*self
, void *closure
)
249 inferior_object
*inf
= (inferior_object
*) self
;
251 INFPY_REQUIRE_VALID (inf
);
253 return PyLong_FromLong (inf
->inferior
->pid
);
257 infpy_get_was_attached (PyObject
*self
, void *closure
)
259 inferior_object
*inf
= (inferior_object
*) self
;
261 INFPY_REQUIRE_VALID (inf
);
262 if (inf
->inferior
->attach_flag
)
268 build_inferior_list (struct inferior
*inf
, void *arg
)
270 PyObject
*list
= arg
;
271 PyObject
*inferior
= inferior_to_inferior_object (inf
);
273 PyList_Append (list
, inferior
);
277 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
278 Returns a tuple of all inferiors. */
280 gdbpy_inferiors (PyObject
*unused
, PyObject
*unused2
)
283 PyObject
*list
, *inferior
;
284 struct inferior
*inf
;
286 list
= PyList_New (0);
290 iterate_over_inferiors (build_inferior_list
, list
);
292 return PyList_AsTuple (list
);
295 /* Membuf and memory manipulation. */
297 /* Implementation of gdb.read_memory (address, length).
298 Returns a Python buffer object with LENGTH bytes of the inferior's
299 memory at ADDRESS. Both arguments are integers. Returns NULL on error,
300 with a python exception set. */
302 infpy_read_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
305 CORE_ADDR addr
, length
;
307 membuf_object
*membuf_obj
;
308 PyObject
*addr_obj
, *length_obj
;
309 struct cleanup
*cleanups
;
310 volatile struct gdb_exception except
;
311 static char *keywords
[] = { "address", "length", NULL
};
313 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OO", keywords
,
314 &addr_obj
, &length_obj
))
317 cleanups
= make_cleanup (null_cleanup
, NULL
);
319 TRY_CATCH (except
, RETURN_MASK_ALL
)
321 if (!get_addr_from_python (addr_obj
, &addr
)
322 || !get_addr_from_python (length_obj
, &length
))
328 buffer
= xmalloc (length
);
329 make_cleanup (xfree
, buffer
);
331 read_memory (addr
, buffer
, length
);
333 if (except
.reason
< 0)
335 do_cleanups (cleanups
);
336 GDB_PY_HANDLE_EXCEPTION (except
);
341 do_cleanups (cleanups
);
345 membuf_obj
= PyObject_New (membuf_object
, &membuf_object_type
);
346 if (membuf_obj
== NULL
)
348 PyErr_SetString (PyExc_MemoryError
,
349 _("Could not allocate memory buffer object."));
350 do_cleanups (cleanups
);
354 discard_cleanups (cleanups
);
356 membuf_obj
->buffer
= buffer
;
357 membuf_obj
->addr
= addr
;
358 membuf_obj
->length
= length
;
360 return PyBuffer_FromReadWriteObject ((PyObject
*) membuf_obj
, 0,
364 /* Implementation of gdb.write_memory (address, buffer [, length]).
365 Writes the contents of BUFFER (a Python object supporting the read
366 buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
367 bytes from BUFFER, or its entire contents if the argument is not
368 provided. The function returns nothing. Returns NULL on error, with
369 a python exception set. */
371 infpy_write_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
373 int buf_len
, error
= 0;
375 CORE_ADDR addr
, length
;
376 PyObject
*addr_obj
, *length_obj
= NULL
;
377 volatile struct gdb_exception except
;
378 static char *keywords
[] = { "address", "buffer", "length", NULL
};
381 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "Os#|O", keywords
,
382 &addr_obj
, &buffer
, &buf_len
,
386 TRY_CATCH (except
, RETURN_MASK_ALL
)
388 if (!get_addr_from_python (addr_obj
, &addr
))
396 else if (!get_addr_from_python (length_obj
, &length
))
401 write_memory (addr
, buffer
, length
);
403 GDB_PY_HANDLE_EXCEPTION (except
);
411 /* Destructor of Membuf objects. */
413 mbpy_dealloc (PyObject
*self
)
415 xfree (((membuf_object
*) self
)->buffer
);
416 self
->ob_type
->tp_free (self
);
419 /* Return a description of the Membuf object. */
421 mbpy_str (PyObject
*self
)
423 membuf_object
*membuf_obj
= (membuf_object
*) self
;
425 return PyString_FromFormat (_("Memory buffer for address %s, \
426 which is %s bytes long."),
427 paddress (python_gdbarch
, membuf_obj
->addr
),
428 pulongest (membuf_obj
->length
));
432 get_read_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
434 membuf_object
*membuf_obj
= (membuf_object
*) self
;
438 PyErr_SetString (PyExc_SystemError
,
439 _("The memory buffer supports only one segment."));
443 *ptrptr
= membuf_obj
->buffer
;
445 return membuf_obj
->length
;
449 get_write_buffer (PyObject
*self
, Py_ssize_t segment
, void **ptrptr
)
451 return get_read_buffer (self
, segment
, ptrptr
);
455 get_seg_count (PyObject
*self
, Py_ssize_t
*lenp
)
458 *lenp
= ((membuf_object
*) self
)->length
;
464 get_char_buffer (PyObject
*self
, Py_ssize_t segment
, char **ptrptr
)
469 ret
= get_read_buffer (self
, segment
, &ptr
);
470 *ptrptr
= (char *) ptr
;
476 gdb.search_memory (address, length, pattern). ADDRESS is the
477 address to start the search. LENGTH specifies the scope of the
478 search from ADDRESS. PATTERN is the pattern to search for (and
479 must be a Python object supporting the buffer protocol).
480 Returns a Python Long object holding the address where the pattern
481 was located, or if the pattern was not found, returns None. Returns NULL
482 on error, with a python exception set. */
484 infpy_search_memory (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
486 CORE_ADDR start_addr
, length
;
487 static char *keywords
[] = { "address", "length", "pattern", NULL
};
488 PyObject
*pattern
, *start_addr_obj
, *length_obj
;
489 volatile struct gdb_exception except
;
490 Py_ssize_t pattern_size
;
492 CORE_ADDR found_addr
;
495 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "OOO", keywords
,
496 &start_addr_obj
, &length_obj
,
500 if (get_addr_from_python (start_addr_obj
, &start_addr
)
501 && get_addr_from_python (length_obj
, &length
))
505 PyErr_SetString (PyExc_ValueError
,
506 _("Search range is empty."));
509 /* Watch for overflows. */
510 else if (length
> CORE_ADDR_MAX
511 || (start_addr
+ length
- 1) < start_addr
)
513 PyErr_SetString (PyExc_ValueError
,
514 _("The search range is too large."));
522 if (!PyObject_CheckReadBuffer (pattern
))
524 PyErr_SetString (PyExc_RuntimeError
,
525 _("The pattern is not a Python buffer."));
530 if (PyObject_AsReadBuffer (pattern
, &buffer
, &pattern_size
) == -1)
533 TRY_CATCH (except
, RETURN_MASK_ALL
)
535 found
= target_search_memory (start_addr
, length
,
536 buffer
, pattern_size
,
539 GDB_PY_HANDLE_EXCEPTION (except
);
542 return PyLong_FromLong (found_addr
);
548 /* Clear the INFERIOR pointer in an Inferior object and clear the
551 py_free_inferior (struct inferior
*inf
, void *datum
)
554 struct cleanup
*cleanup
;
555 inferior_object
*inf_obj
= datum
;
556 struct threadlist_entry
*th_entry
, *th_tmp
;
558 cleanup
= ensure_python_env (python_gdbarch
, python_language
);
560 inf_obj
->inferior
= NULL
;
562 /* Deallocate threads list. */
563 for (th_entry
= inf_obj
->threads
; th_entry
!= NULL
;)
565 Py_DECREF (th_entry
->thread_obj
);
568 th_entry
= th_entry
->next
;
572 inf_obj
->nthreads
= 0;
574 Py_DECREF ((PyObject
*) inf_obj
);
575 do_cleanups (cleanup
);
579 gdbpy_initialize_inferior (void)
581 if (PyType_Ready (&inferior_object_type
) < 0)
584 Py_INCREF (&inferior_object_type
);
585 PyModule_AddObject (gdb_module
, "Inferior",
586 (PyObject
*) &inferior_object_type
);
589 register_inferior_data_with_cleanup (py_free_inferior
);
591 observer_attach_new_thread (add_thread_object
);
592 observer_attach_thread_exit (delete_thread_object
);
594 if (PyType_Ready (&membuf_object_type
) < 0)
597 Py_INCREF (&membuf_object_type
);
598 PyModule_AddObject (gdb_module
, "Membuf", (PyObject
*)
599 &membuf_object_type
);
602 static PyGetSetDef inferior_object_getset
[] =
604 { "num", infpy_get_num
, NULL
, "ID of inferior, as assigned by GDB.", NULL
},
605 { "pid", infpy_get_pid
, NULL
, "PID of inferior, as assigned by the OS.",
607 { "was_attached", infpy_get_was_attached
, NULL
,
608 "True if the inferior was created using 'attach'.", NULL
},
612 static PyMethodDef inferior_object_methods
[] =
614 { "threads", infpy_threads
, METH_NOARGS
,
615 "Return all the threads of this inferior." },
616 { "read_memory", (PyCFunction
) infpy_read_memory
,
617 METH_VARARGS
| METH_KEYWORDS
,
618 "read_memory (address, length) -> buffer\n\
619 Return a buffer object for reading from the inferior's memory." },
620 { "write_memory", (PyCFunction
) infpy_write_memory
,
621 METH_VARARGS
| METH_KEYWORDS
,
622 "write_memory (address, buffer [, length])\n\
623 Write the given buffer object to the inferior's memory." },
624 { "search_memory", (PyCFunction
) infpy_search_memory
,
625 METH_VARARGS
| METH_KEYWORDS
,
626 "search_memory (address, length, pattern) -> long\n\
627 Return a long with the address of a match, or None." },
631 static PyTypeObject inferior_object_type
=
633 PyObject_HEAD_INIT (NULL
)
635 "gdb.Inferior", /* tp_name */
636 sizeof (inferior_object
), /* tp_basicsize */
644 0, /* tp_as_number */
645 0, /* tp_as_sequence */
646 0, /* tp_as_mapping */
652 0, /* tp_as_buffer */
653 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /* tp_flags */
654 "GDB inferior object", /* tp_doc */
657 0, /* tp_richcompare */
658 0, /* tp_weaklistoffset */
661 inferior_object_methods
, /* tp_methods */
663 inferior_object_getset
, /* tp_getset */
666 0, /* tp_descr_get */
667 0, /* tp_descr_set */
668 0, /* tp_dictoffset */
673 /* Python doesn't provide a decent way to get compatibility here. */
674 #if HAVE_LIBPYTHON2_4
675 #define CHARBUFFERPROC_NAME getcharbufferproc
677 #define CHARBUFFERPROC_NAME charbufferproc
680 static PyBufferProcs buffer_procs
= {
684 /* The cast here works around a difference between Python 2.4 and
686 (CHARBUFFERPROC_NAME
) get_char_buffer
689 static PyTypeObject membuf_object_type
= {
690 PyObject_HEAD_INIT (NULL
)
692 "gdb.Membuf", /*tp_name*/
693 sizeof (membuf_object
), /*tp_basicsize*/
695 mbpy_dealloc
, /*tp_dealloc*/
702 0, /*tp_as_sequence*/
709 &buffer_procs
, /*tp_as_buffer*/
710 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
711 "GDB memory buffer object", /*tp_doc*/
714 0, /* tp_richcompare */
715 0, /* tp_weaklistoffset */
723 0, /* tp_descr_get */
724 0, /* tp_descr_set */
725 0, /* tp_dictoffset */
728 PyType_GenericNew
/* tp_new */