run copyright.sh for 2011.
[deliverable/binutils-gdb.git] / gdb / python / py-inferior.c
1 /* Python interface to inferiors.
2
3 Copyright (C) 2009, 2010, 2011 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 if (pid == 0)
134 return NULL;
135
136 inf_obj = find_inferior_object (pid);
137
138 if (inf_obj)
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;
143
144 return NULL;
145 }
146
147 static void
148 add_thread_object (struct thread_info *tp)
149 {
150 struct cleanup *cleanup;
151 thread_object *thread_obj;
152 inferior_object *inf_obj;
153 struct threadlist_entry *entry;
154
155 cleanup = ensure_python_env (python_gdbarch, python_language);
156
157 thread_obj = create_thread_object (tp);
158 if (!thread_obj)
159 {
160 gdbpy_print_stack ();
161 do_cleanups (cleanup);
162 return;
163 }
164
165 inf_obj = (inferior_object *) thread_obj->inf_obj;
166
167 entry = xmalloc (sizeof (struct threadlist_entry));
168 entry->thread_obj = thread_obj;
169 entry->next = inf_obj->threads;
170
171 inf_obj->threads = entry;
172 inf_obj->nthreads++;
173
174 do_cleanups (cleanup);
175 }
176
177 static void
178 delete_thread_object (struct thread_info *tp, int ignore)
179 {
180 struct cleanup *cleanup;
181 inferior_object *inf_obj;
182 thread_object *thread_obj;
183 struct threadlist_entry **entry, *tmp;
184
185 inf_obj = (inferior_object *) find_inferior_object (PIDGET(tp->ptid));
186 if (!inf_obj)
187 return;
188
189 /* Find thread entry in its inferior's thread_list. */
190 for (entry = &inf_obj->threads; *entry != NULL; entry =
191 &(*entry)->next)
192 if ((*entry)->thread_obj->thread == tp)
193 break;
194
195 if (!*entry)
196 return;
197
198 cleanup = ensure_python_env (python_gdbarch, python_language);
199
200 tmp = *entry;
201 tmp->thread_obj->thread = NULL;
202
203 *entry = (*entry)->next;
204 inf_obj->nthreads--;
205
206 Py_DECREF (tmp->thread_obj);
207 xfree (tmp);
208
209 do_cleanups (cleanup);
210 }
211
212 static PyObject *
213 infpy_threads (PyObject *self, PyObject *args)
214 {
215 int i;
216 struct threadlist_entry *entry;
217 inferior_object *inf_obj = (inferior_object *) self;
218 PyObject *tuple;
219
220 INFPY_REQUIRE_VALID (inf_obj);
221
222 tuple = PyTuple_New (inf_obj->nthreads);
223 if (!tuple)
224 return NULL;
225
226 for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads;
227 i++, entry = entry->next)
228 {
229 Py_INCREF (entry->thread_obj);
230 PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj);
231 }
232
233 return tuple;
234 }
235
236 static PyObject *
237 infpy_get_num (PyObject *self, void *closure)
238 {
239 inferior_object *inf = (inferior_object *) self;
240
241 INFPY_REQUIRE_VALID (inf);
242
243 return PyLong_FromLong (inf->inferior->num);
244 }
245
246 static PyObject *
247 infpy_get_pid (PyObject *self, void *closure)
248 {
249 inferior_object *inf = (inferior_object *) self;
250
251 INFPY_REQUIRE_VALID (inf);
252
253 return PyLong_FromLong (inf->inferior->pid);
254 }
255
256 static PyObject *
257 infpy_get_was_attached (PyObject *self, void *closure)
258 {
259 inferior_object *inf = (inferior_object *) self;
260
261 INFPY_REQUIRE_VALID (inf);
262 if (inf->inferior->attach_flag)
263 Py_RETURN_TRUE;
264 Py_RETURN_FALSE;
265 }
266
267 static int
268 build_inferior_list (struct inferior *inf, void *arg)
269 {
270 PyObject *list = arg;
271 PyObject *inferior = inferior_to_inferior_object (inf);
272
273 PyList_Append (list, inferior);
274 return 0;
275 }
276
277 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
278 Returns a tuple of all inferiors. */
279 PyObject *
280 gdbpy_inferiors (PyObject *unused, PyObject *unused2)
281 {
282 int i = 0;
283 PyObject *list, *inferior;
284 struct inferior *inf;
285
286 list = PyList_New (0);
287 if (!list)
288 return NULL;
289
290 iterate_over_inferiors (build_inferior_list, list);
291
292 return PyList_AsTuple (list);
293 }
294
295 /* Membuf and memory manipulation. */
296
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. */
301 static PyObject *
302 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
303 {
304 int error = 0;
305 CORE_ADDR addr, length;
306 void *buffer = NULL;
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 };
312
313 if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
314 &addr_obj, &length_obj))
315 return NULL;
316
317 cleanups = make_cleanup (null_cleanup, NULL);
318
319 TRY_CATCH (except, RETURN_MASK_ALL)
320 {
321 if (!get_addr_from_python (addr_obj, &addr)
322 || !get_addr_from_python (length_obj, &length))
323 {
324 error = 1;
325 break;
326 }
327
328 buffer = xmalloc (length);
329 make_cleanup (xfree, buffer);
330
331 read_memory (addr, buffer, length);
332 }
333 if (except.reason < 0)
334 {
335 do_cleanups (cleanups);
336 GDB_PY_HANDLE_EXCEPTION (except);
337 }
338
339 if (error)
340 {
341 do_cleanups (cleanups);
342 return NULL;
343 }
344
345 membuf_obj = PyObject_New (membuf_object, &membuf_object_type);
346 if (membuf_obj == NULL)
347 {
348 PyErr_SetString (PyExc_MemoryError,
349 _("Could not allocate memory buffer object."));
350 do_cleanups (cleanups);
351 return NULL;
352 }
353
354 discard_cleanups (cleanups);
355
356 membuf_obj->buffer = buffer;
357 membuf_obj->addr = addr;
358 membuf_obj->length = length;
359
360 return PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
361 Py_END_OF_BUFFER);
362 }
363
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. */
370 static PyObject *
371 infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
372 {
373 int buf_len, error = 0;
374 const char *buffer;
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 };
379
380
381 if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
382 &addr_obj, &buffer, &buf_len,
383 &length_obj))
384 return NULL;
385
386 TRY_CATCH (except, RETURN_MASK_ALL)
387 {
388 if (!get_addr_from_python (addr_obj, &addr))
389 {
390 error = 1;
391 break;
392 }
393
394 if (!length_obj)
395 length = buf_len;
396 else if (!get_addr_from_python (length_obj, &length))
397 {
398 error = 1;
399 break;
400 }
401 write_memory (addr, buffer, length);
402 }
403 GDB_PY_HANDLE_EXCEPTION (except);
404
405 if (error)
406 return NULL;
407
408 Py_RETURN_NONE;
409 }
410
411 /* Destructor of Membuf objects. */
412 static void
413 mbpy_dealloc (PyObject *self)
414 {
415 xfree (((membuf_object *) self)->buffer);
416 self->ob_type->tp_free (self);
417 }
418
419 /* Return a description of the Membuf object. */
420 static PyObject *
421 mbpy_str (PyObject *self)
422 {
423 membuf_object *membuf_obj = (membuf_object *) self;
424
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));
429 }
430
431 static Py_ssize_t
432 get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
433 {
434 membuf_object *membuf_obj = (membuf_object *) self;
435
436 if (segment)
437 {
438 PyErr_SetString (PyExc_SystemError,
439 _("The memory buffer supports only one segment."));
440 return -1;
441 }
442
443 *ptrptr = membuf_obj->buffer;
444
445 return membuf_obj->length;
446 }
447
448 static Py_ssize_t
449 get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
450 {
451 return get_read_buffer (self, segment, ptrptr);
452 }
453
454 static Py_ssize_t
455 get_seg_count (PyObject *self, Py_ssize_t *lenp)
456 {
457 if (lenp)
458 *lenp = ((membuf_object *) self)->length;
459
460 return 1;
461 }
462
463 static Py_ssize_t
464 get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
465 {
466 void *ptr = NULL;
467 Py_ssize_t ret;
468
469 ret = get_read_buffer (self, segment, &ptr);
470 *ptrptr = (char *) ptr;
471
472 return ret;
473 }
474
475 /* Implementation of
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. */
483 static PyObject *
484 infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
485 {
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;
491 const void *buffer;
492 CORE_ADDR found_addr;
493 int found = 0;
494
495 if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
496 &start_addr_obj, &length_obj,
497 &pattern))
498 return NULL;
499
500 if (get_addr_from_python (start_addr_obj, &start_addr)
501 && get_addr_from_python (length_obj, &length))
502 {
503 if (!length)
504 {
505 PyErr_SetString (PyExc_ValueError,
506 _("Search range is empty."));
507 return NULL;
508 }
509 /* Watch for overflows. */
510 else if (length > CORE_ADDR_MAX
511 || (start_addr + length - 1) < start_addr)
512 {
513 PyErr_SetString (PyExc_ValueError,
514 _("The search range is too large."));
515
516 return NULL;
517 }
518 }
519 else
520 return NULL;
521
522 if (!PyObject_CheckReadBuffer (pattern))
523 {
524 PyErr_SetString (PyExc_RuntimeError,
525 _("The pattern is not a Python buffer."));
526
527 return NULL;
528 }
529
530 if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1)
531 return NULL;
532
533 TRY_CATCH (except, RETURN_MASK_ALL)
534 {
535 found = target_search_memory (start_addr, length,
536 buffer, pattern_size,
537 &found_addr);
538 }
539 GDB_PY_HANDLE_EXCEPTION (except);
540
541 if (found)
542 return PyLong_FromLong (found_addr);
543 else
544 Py_RETURN_NONE;
545 }
546
547
548 /* Clear the INFERIOR pointer in an Inferior object and clear the
549 thread list. */
550 static void
551 py_free_inferior (struct inferior *inf, void *datum)
552 {
553
554 struct cleanup *cleanup;
555 inferior_object *inf_obj = datum;
556 struct threadlist_entry *th_entry, *th_tmp;
557
558 cleanup = ensure_python_env (python_gdbarch, python_language);
559
560 inf_obj->inferior = NULL;
561
562 /* Deallocate threads list. */
563 for (th_entry = inf_obj->threads; th_entry != NULL;)
564 {
565 Py_DECREF (th_entry->thread_obj);
566
567 th_tmp = th_entry;
568 th_entry = th_entry->next;
569 xfree (th_tmp);
570 }
571
572 inf_obj->nthreads = 0;
573
574 Py_DECREF ((PyObject *) inf_obj);
575 do_cleanups (cleanup);
576 }
577
578 void
579 gdbpy_initialize_inferior (void)
580 {
581 if (PyType_Ready (&inferior_object_type) < 0)
582 return;
583
584 Py_INCREF (&inferior_object_type);
585 PyModule_AddObject (gdb_module, "Inferior",
586 (PyObject *) &inferior_object_type);
587
588 infpy_inf_data_key =
589 register_inferior_data_with_cleanup (py_free_inferior);
590
591 observer_attach_new_thread (add_thread_object);
592 observer_attach_thread_exit (delete_thread_object);
593
594 if (PyType_Ready (&membuf_object_type) < 0)
595 return;
596
597 Py_INCREF (&membuf_object_type);
598 PyModule_AddObject (gdb_module, "Membuf", (PyObject *)
599 &membuf_object_type);
600 }
601
602 static PyGetSetDef inferior_object_getset[] =
603 {
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.",
606 NULL },
607 { "was_attached", infpy_get_was_attached, NULL,
608 "True if the inferior was created using 'attach'.", NULL },
609 { NULL }
610 };
611
612 static PyMethodDef inferior_object_methods[] =
613 {
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." },
628 { NULL }
629 };
630
631 static PyTypeObject inferior_object_type =
632 {
633 PyObject_HEAD_INIT (NULL)
634 0, /* ob_size */
635 "gdb.Inferior", /* tp_name */
636 sizeof (inferior_object), /* tp_basicsize */
637 0, /* tp_itemsize */
638 0, /* tp_dealloc */
639 0, /* tp_print */
640 0, /* tp_getattr */
641 0, /* tp_setattr */
642 0, /* tp_compare */
643 0, /* tp_repr */
644 0, /* tp_as_number */
645 0, /* tp_as_sequence */
646 0, /* tp_as_mapping */
647 0, /* tp_hash */
648 0, /* tp_call */
649 0, /* tp_str */
650 0, /* tp_getattro */
651 0, /* tp_setattro */
652 0, /* tp_as_buffer */
653 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /* tp_flags */
654 "GDB inferior object", /* tp_doc */
655 0, /* tp_traverse */
656 0, /* tp_clear */
657 0, /* tp_richcompare */
658 0, /* tp_weaklistoffset */
659 0, /* tp_iter */
660 0, /* tp_iternext */
661 inferior_object_methods, /* tp_methods */
662 0, /* tp_members */
663 inferior_object_getset, /* tp_getset */
664 0, /* tp_base */
665 0, /* tp_dict */
666 0, /* tp_descr_get */
667 0, /* tp_descr_set */
668 0, /* tp_dictoffset */
669 0, /* tp_init */
670 0 /* tp_alloc */
671 };
672
673 /* Python doesn't provide a decent way to get compatibility here. */
674 #if HAVE_LIBPYTHON2_4
675 #define CHARBUFFERPROC_NAME getcharbufferproc
676 #else
677 #define CHARBUFFERPROC_NAME charbufferproc
678 #endif
679
680 static PyBufferProcs buffer_procs = {
681 get_read_buffer,
682 get_write_buffer,
683 get_seg_count,
684 /* The cast here works around a difference between Python 2.4 and
685 Python 2.5. */
686 (CHARBUFFERPROC_NAME) get_char_buffer
687 };
688
689 static PyTypeObject membuf_object_type = {
690 PyObject_HEAD_INIT (NULL)
691 0, /*ob_size*/
692 "gdb.Membuf", /*tp_name*/
693 sizeof (membuf_object), /*tp_basicsize*/
694 0, /*tp_itemsize*/
695 mbpy_dealloc, /*tp_dealloc*/
696 0, /*tp_print*/
697 0, /*tp_getattr*/
698 0, /*tp_setattr*/
699 0, /*tp_compare*/
700 0, /*tp_repr*/
701 0, /*tp_as_number*/
702 0, /*tp_as_sequence*/
703 0, /*tp_as_mapping*/
704 0, /*tp_hash */
705 0, /*tp_call*/
706 mbpy_str, /*tp_str*/
707 0, /*tp_getattro*/
708 0, /*tp_setattro*/
709 &buffer_procs, /*tp_as_buffer*/
710 Py_TPFLAGS_DEFAULT, /*tp_flags*/
711 "GDB memory buffer object", /*tp_doc*/
712 0, /* tp_traverse */
713 0, /* tp_clear */
714 0, /* tp_richcompare */
715 0, /* tp_weaklistoffset */
716 0, /* tp_iter */
717 0, /* tp_iternext */
718 0, /* tp_methods */
719 0, /* tp_members */
720 0, /* tp_getset */
721 0, /* tp_base */
722 0, /* tp_dict */
723 0, /* tp_descr_get */
724 0, /* tp_descr_set */
725 0, /* tp_dictoffset */
726 0, /* tp_init */
727 0, /* tp_alloc */
728 PyType_GenericNew /* tp_new */
729 };
This page took 0.045355 seconds and 5 git commands to generate.