3a0feab19a4b83bbcc1dc67981e594a39f154571
[deliverable/binutils-gdb.git] / gdb / python / py-inferior.c
1 /* Python interface to inferiors.
2
3 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "exceptions.h"
22 #include "gdbcore.h"
23 #include "gdbthread.h"
24 #include "inferior.h"
25 #include "observer.h"
26 #include "python-internal.h"
27 #include "arch-utils.h"
28 #include "language.h"
29
30 struct threadlist_entry {
31 thread_object *thread_obj;
32 struct threadlist_entry *next;
33 };
34
35 typedef struct
36 {
37 PyObject_HEAD
38
39 /* The inferior we represent. */
40 struct inferior *inferior;
41
42 /* thread_object instances under this inferior. This list owns a
43 reference to each object it contains. */
44 struct threadlist_entry *threads;
45
46 /* Number of threads in the list. */
47 int nthreads;
48 } inferior_object;
49
50 static PyTypeObject inferior_object_type;
51
52 static const struct inferior_data *infpy_inf_data_key;
53
54 typedef struct {
55 PyObject_HEAD
56 void *buffer;
57
58 /* These are kept just for mbpy_str. */
59 CORE_ADDR addr;
60 CORE_ADDR length;
61 } membuf_object;
62
63 static PyTypeObject membuf_object_type;
64
65 /* Require that INFERIOR be a valid inferior ID. */
66 #define INFPY_REQUIRE_VALID(Inferior) \
67 do { \
68 if (!Inferior->inferior) \
69 { \
70 PyErr_SetString (PyExc_RuntimeError, \
71 _("Inferior no longer exists.")); \
72 return NULL; \
73 } \
74 } while (0)
75
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. */
79 PyObject *
80 inferior_to_inferior_object (struct inferior *inferior)
81 {
82 inferior_object *inf_obj;
83
84 inf_obj = inferior_data (inferior, infpy_inf_data_key);
85 if (!inf_obj)
86 {
87 struct cleanup *cleanup;
88 cleanup = ensure_python_env (python_gdbarch, python_language);
89
90 inf_obj = PyObject_New (inferior_object, &inferior_object_type);
91 if (!inf_obj)
92 {
93 do_cleanups (cleanup);
94 return NULL;
95 }
96
97 inf_obj->inferior = inferior;
98 inf_obj->threads = NULL;
99 inf_obj->nthreads = 0;
100
101 set_inferior_data (inferior, infpy_inf_data_key, inf_obj);
102
103 do_cleanups (cleanup);
104 }
105
106 return (PyObject *) inf_obj;
107 }
108
109 /* Finds the Python Inferior object for the given PID. Returns a
110 borrowed reference, or NULL if PID does not match any inferior
111 obect.
112 */
113 PyObject *
114 find_inferior_object (int pid)
115 {
116 struct inflist_entry *p;
117 struct inferior *inf = find_inferior_pid (pid);
118
119 if (inf)
120 return inferior_to_inferior_object (inf);
121
122 return NULL;
123 }
124
125 thread_object *
126 find_thread_object (ptid_t ptid)
127 {
128 int pid;
129 struct threadlist_entry *thread;
130 PyObject *inf_obj;
131
132 pid = PIDGET (ptid);
133 inf_obj = find_inferior_object (pid);
134
135 if (inf_obj)
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;
140
141 return NULL;
142 }
143
144 static void
145 add_thread_object (struct thread_info *tp)
146 {
147 struct cleanup *cleanup;
148 thread_object *thread_obj;
149 inferior_object *inf_obj;
150 struct threadlist_entry *entry;
151
152 cleanup = ensure_python_env (python_gdbarch, python_language);
153
154 thread_obj = create_thread_object (tp);
155 if (!thread_obj)
156 {
157 gdbpy_print_stack ();
158 do_cleanups (cleanup);
159 return;
160 }
161
162 inf_obj = (inferior_object *) thread_obj->inf_obj;
163
164 entry = xmalloc (sizeof (struct threadlist_entry));
165 entry->thread_obj = thread_obj;
166 entry->next = inf_obj->threads;
167
168 inf_obj->threads = entry;
169 inf_obj->nthreads++;
170
171 do_cleanups (cleanup);
172 }
173
174 static void
175 delete_thread_object (struct thread_info *tp, int ignore)
176 {
177 struct cleanup *cleanup;
178 inferior_object *inf_obj;
179 thread_object *thread_obj;
180 struct threadlist_entry **entry, *tmp;
181
182 inf_obj = (inferior_object *) find_inferior_object (PIDGET(tp->ptid));
183 if (!inf_obj)
184 return;
185
186 /* Find thread entry in its inferior's thread_list. */
187 for (entry = &inf_obj->threads; *entry != NULL; entry =
188 &(*entry)->next)
189 if ((*entry)->thread_obj->thread == tp)
190 break;
191
192 if (!*entry)
193 return;
194
195 cleanup = ensure_python_env (python_gdbarch, python_language);
196
197 tmp = *entry;
198 tmp->thread_obj->thread = NULL;
199
200 *entry = (*entry)->next;
201 inf_obj->nthreads--;
202
203 Py_DECREF (tmp->thread_obj);
204 xfree (tmp);
205
206 do_cleanups (cleanup);
207 }
208
209 static PyObject *
210 infpy_threads (PyObject *self, PyObject *args)
211 {
212 int i;
213 struct threadlist_entry *entry;
214 inferior_object *inf_obj = (inferior_object *) self;
215 PyObject *tuple;
216
217 INFPY_REQUIRE_VALID (inf_obj);
218
219 tuple = PyTuple_New (inf_obj->nthreads);
220 if (!tuple)
221 return NULL;
222
223 for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads;
224 i++, entry = entry->next)
225 {
226 Py_INCREF (entry->thread_obj);
227 PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj);
228 }
229
230 return tuple;
231 }
232
233 static PyObject *
234 infpy_get_num (PyObject *self, void *closure)
235 {
236 inferior_object *inf = (inferior_object *) self;
237
238 INFPY_REQUIRE_VALID (inf);
239
240 return PyLong_FromLong (inf->inferior->num);
241 }
242
243 static PyObject *
244 infpy_get_pid (PyObject *self, void *closure)
245 {
246 inferior_object *inf = (inferior_object *) self;
247
248 INFPY_REQUIRE_VALID (inf);
249
250 return PyLong_FromLong (inf->inferior->pid);
251 }
252
253 static PyObject *
254 infpy_get_was_attached (PyObject *self, void *closure)
255 {
256 inferior_object *inf = (inferior_object *) self;
257
258 INFPY_REQUIRE_VALID (inf);
259 if (inf->inferior->attach_flag)
260 Py_RETURN_TRUE;
261 Py_RETURN_FALSE;
262 }
263
264 static int
265 build_inferior_list (struct inferior *inf, void *arg)
266 {
267 PyObject *list = arg;
268 PyObject *inferior = inferior_to_inferior_object (inf);
269
270 PyList_Append (list, inferior);
271 return 0;
272 }
273
274 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
275 Returns a tuple of all inferiors. */
276 PyObject *
277 gdbpy_inferiors (PyObject *unused, PyObject *unused2)
278 {
279 int i = 0;
280 PyObject *list, *inferior;
281 struct inferior *inf;
282
283 list = PyList_New (0);
284 if (!list)
285 return NULL;
286
287 iterate_over_inferiors (build_inferior_list, list);
288
289 return PyList_AsTuple (list);
290 }
291
292 /* Membuf and memory manipulation. */
293
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. */
297 static PyObject *
298 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
299 {
300 int error = 0;
301 CORE_ADDR addr, length;
302 void *buffer = NULL;
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 };
308
309 if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
310 &addr_obj, &length_obj))
311 return NULL;
312
313 cleanups = make_cleanup (null_cleanup, NULL);
314
315 TRY_CATCH (except, RETURN_MASK_ALL)
316 {
317 if (!get_addr_from_python (addr_obj, &addr)
318 || !get_addr_from_python (length_obj, &length))
319 {
320 error = 1;
321 break;
322 }
323
324 buffer = xmalloc (length);
325 make_cleanup (xfree, buffer);
326
327 read_memory (addr, buffer, length);
328 }
329 if (except.reason < 0)
330 {
331 do_cleanups (cleanups);
332 GDB_PY_HANDLE_EXCEPTION (except);
333 }
334
335 if (error)
336 {
337 do_cleanups (cleanups);
338 return NULL;
339 }
340
341 membuf_obj = PyObject_New (membuf_object, &membuf_object_type);
342 if (membuf_obj == NULL)
343 {
344 PyErr_SetString (PyExc_MemoryError,
345 _("Could not allocate memory buffer object."));
346 do_cleanups (cleanups);
347 return NULL;
348 }
349
350 discard_cleanups (cleanups);
351
352 membuf_obj->buffer = buffer;
353 membuf_obj->addr = addr;
354 membuf_obj->length = length;
355
356 return PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
357 Py_END_OF_BUFFER);
358 }
359
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. */
365 static PyObject *
366 infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
367 {
368 int buf_len, error = 0;
369 const char *buffer;
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 };
374
375
376 if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
377 &addr_obj, &buffer, &buf_len,
378 &length_obj))
379 return NULL;
380
381 TRY_CATCH (except, RETURN_MASK_ALL)
382 {
383 if (!get_addr_from_python (addr_obj, &addr))
384 {
385 error = 1;
386 break;
387 }
388
389 if (!length_obj)
390 length = buf_len;
391 else if (!get_addr_from_python (length_obj, &length))
392 {
393 error = 1;
394 break;
395 }
396 write_memory (addr, buffer, length);
397 }
398 GDB_PY_HANDLE_EXCEPTION (except);
399
400 if (error)
401 return NULL;
402
403 Py_RETURN_NONE;
404 }
405
406 /* Destructor of Membuf objects. */
407 static void
408 mbpy_dealloc (PyObject *self)
409 {
410 xfree (((membuf_object *) self)->buffer);
411 self->ob_type->tp_free (self);
412 }
413
414 /* Return a description of the Membuf object. */
415 static PyObject *
416 mbpy_str (PyObject *self)
417 {
418 membuf_object *membuf_obj = (membuf_object *) self;
419
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));
424 }
425
426 static Py_ssize_t
427 get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
428 {
429 membuf_object *membuf_obj = (membuf_object *) self;
430
431 if (segment)
432 {
433 PyErr_SetString (PyExc_SystemError,
434 _("The memory buffer supports only one segment."));
435 return -1;
436 }
437
438 *ptrptr = membuf_obj->buffer;
439
440 return membuf_obj->length;
441 }
442
443 static Py_ssize_t
444 get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
445 {
446 return get_read_buffer (self, segment, ptrptr);
447 }
448
449 static Py_ssize_t
450 get_seg_count (PyObject *self, Py_ssize_t *lenp)
451 {
452 if (lenp)
453 *lenp = ((membuf_object *) self)->length;
454
455 return 1;
456 }
457
458 static Py_ssize_t
459 get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
460 {
461 void *ptr = NULL;
462 Py_ssize_t ret;
463
464 ret = get_read_buffer (self, segment, &ptr);
465 *ptrptr = (char *) ptr;
466
467 return ret;
468 }
469
470 /* Implementation of
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. */
477 static PyObject *
478 infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
479 {
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;
485 const void *buffer;
486 CORE_ADDR found_addr;
487 int found = 0;
488
489 if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
490 &start_addr_obj, &length_obj,
491 &pattern))
492 return NULL;
493
494 if (get_addr_from_python (start_addr_obj, &start_addr)
495 && get_addr_from_python (length_obj, &length))
496 {
497 if (!length)
498 {
499 PyErr_SetString (PyExc_ValueError,
500 _("Search range is empty."));
501 return NULL;
502 }
503 /* Watch for overflows. */
504 else if (length > CORE_ADDR_MAX
505 || (start_addr + length - 1) < start_addr)
506 {
507 PyErr_SetString (PyExc_ValueError,
508 _("The search range is too large."));
509
510 return NULL;
511 }
512 }
513 else
514 {
515 PyErr_SetString (PyExc_RuntimeError,
516 _("Cannot get search address/range from Python."));
517
518 return NULL;
519 }
520
521 if (!PyObject_CheckReadBuffer (pattern))
522 {
523 PyErr_SetString (PyExc_RuntimeError,
524 _("The pattern is not a Python buffer."));
525
526 return NULL;
527 }
528
529 if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1)
530 return NULL;
531
532 TRY_CATCH (except, RETURN_MASK_ALL)
533 {
534 found = target_search_memory (start_addr, length,
535 buffer, pattern_size,
536 &found_addr);
537 }
538 GDB_PY_HANDLE_EXCEPTION (except);
539
540 if (found)
541 return PyLong_FromLong (found_addr);
542 else
543 Py_RETURN_NONE;
544 }
545
546
547 /* Clear the INFERIOR pointer in an Inferior object and clear the
548 thread list. */
549 static void
550 py_free_inferior (struct inferior *inf, void *datum)
551 {
552
553 struct cleanup *cleanup;
554 inferior_object *inf_obj = datum;
555 struct threadlist_entry *th_entry, *th_tmp;
556
557 cleanup = ensure_python_env (python_gdbarch, python_language);
558
559 inf_obj->inferior = NULL;
560
561 /* Deallocate threads list. */
562 for (th_entry = inf_obj->threads; th_entry != NULL;)
563 {
564 Py_DECREF (th_entry->thread_obj);
565
566 th_tmp = th_entry;
567 th_entry = th_entry->next;
568 xfree (th_tmp);
569 }
570
571 inf_obj->nthreads = 0;
572
573 Py_DECREF ((PyObject *) inf_obj);
574 do_cleanups (cleanup);
575 }
576
577 void
578 gdbpy_initialize_inferior (void)
579 {
580 if (PyType_Ready (&inferior_object_type) < 0)
581 return;
582
583 Py_INCREF (&inferior_object_type);
584 PyModule_AddObject (gdb_module, "Inferior",
585 (PyObject *) &inferior_object_type);
586
587 infpy_inf_data_key =
588 register_inferior_data_with_cleanup (py_free_inferior);
589
590 observer_attach_new_thread (add_thread_object);
591 observer_attach_thread_exit (delete_thread_object);
592
593 if (PyType_Ready (&membuf_object_type) < 0)
594 return;
595
596 Py_INCREF (&membuf_object_type);
597 PyModule_AddObject (gdb_module, "Membuf", (PyObject *)
598 &membuf_object_type);
599 }
600
601 static PyGetSetDef inferior_object_getset[] =
602 {
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.",
605 NULL },
606 { "was_attached", infpy_get_was_attached, NULL,
607 "True if the inferior was created using 'attach'.", NULL },
608 { NULL }
609 };
610
611 static PyMethodDef inferior_object_methods[] =
612 {
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." },
627 { NULL }
628 };
629
630 static PyTypeObject inferior_object_type =
631 {
632 PyObject_HEAD_INIT (NULL)
633 0, /* ob_size */
634 "gdb.Inferior", /* tp_name */
635 sizeof (inferior_object), /* tp_basicsize */
636 0, /* tp_itemsize */
637 0, /* tp_dealloc */
638 0, /* tp_print */
639 0, /* tp_getattr */
640 0, /* tp_setattr */
641 0, /* tp_compare */
642 0, /* tp_repr */
643 0, /* tp_as_number */
644 0, /* tp_as_sequence */
645 0, /* tp_as_mapping */
646 0, /* tp_hash */
647 0, /* tp_call */
648 0, /* tp_str */
649 0, /* tp_getattro */
650 0, /* tp_setattro */
651 0, /* tp_as_buffer */
652 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /* tp_flags */
653 "GDB inferior object", /* tp_doc */
654 0, /* tp_traverse */
655 0, /* tp_clear */
656 0, /* tp_richcompare */
657 0, /* tp_weaklistoffset */
658 0, /* tp_iter */
659 0, /* tp_iternext */
660 inferior_object_methods, /* tp_methods */
661 0, /* tp_members */
662 inferior_object_getset, /* tp_getset */
663 0, /* tp_base */
664 0, /* tp_dict */
665 0, /* tp_descr_get */
666 0, /* tp_descr_set */
667 0, /* tp_dictoffset */
668 0, /* tp_init */
669 0 /* tp_alloc */
670 };
671
672 /* Python doesn't provide a decent way to get compatibility here. */
673 #if HAVE_LIBPYTHON2_4
674 #define CHARBUFFERPROC_NAME getcharbufferproc
675 #else
676 #define CHARBUFFERPROC_NAME charbufferproc
677 #endif
678
679 static PyBufferProcs buffer_procs = {
680 get_read_buffer,
681 get_write_buffer,
682 get_seg_count,
683 /* The cast here works around a difference between Python 2.4 and
684 Python 2.5. */
685 (CHARBUFFERPROC_NAME) get_char_buffer
686 };
687
688 static PyTypeObject membuf_object_type = {
689 PyObject_HEAD_INIT (NULL)
690 0, /*ob_size*/
691 "gdb.Membuf", /*tp_name*/
692 sizeof (membuf_object), /*tp_basicsize*/
693 0, /*tp_itemsize*/
694 mbpy_dealloc, /*tp_dealloc*/
695 0, /*tp_print*/
696 0, /*tp_getattr*/
697 0, /*tp_setattr*/
698 0, /*tp_compare*/
699 0, /*tp_repr*/
700 0, /*tp_as_number*/
701 0, /*tp_as_sequence*/
702 0, /*tp_as_mapping*/
703 0, /*tp_hash */
704 0, /*tp_call*/
705 mbpy_str, /*tp_str*/
706 0, /*tp_getattro*/
707 0, /*tp_setattro*/
708 &buffer_procs, /*tp_as_buffer*/
709 Py_TPFLAGS_DEFAULT, /*tp_flags*/
710 "GDB memory buffer object", /*tp_doc*/
711 0, /* tp_traverse */
712 0, /* tp_clear */
713 0, /* tp_richcompare */
714 0, /* tp_weaklistoffset */
715 0, /* tp_iter */
716 0, /* tp_iternext */
717 0, /* tp_methods */
718 0, /* tp_members */
719 0, /* tp_getset */
720 0, /* tp_base */
721 0, /* tp_dict */
722 0, /* tp_descr_get */
723 0, /* tp_descr_set */
724 0, /* tp_dictoffset */
725 0, /* tp_init */
726 0, /* tp_alloc */
727 PyType_GenericNew /* tp_new */
728 };
This page took 0.092082 seconds and 4 git commands to generate.