GDB (xrefs)
py-inferior.c
Go to the documentation of this file.
1 /* Python interface to inferiors.
2 
3  Copyright (C) 2009-2015 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 "gdbcore.h"
22 #include "gdbthread.h"
23 #include "inferior.h"
24 #include "objfiles.h"
25 #include "observer.h"
26 #include "python-internal.h"
27 #include "arch-utils.h"
28 #include "language.h"
29 #include "gdb_signals.h"
30 #include "py-event.h"
31 #include "py-stopevent.h"
32 
36 };
37 
38 typedef struct
39 {
40  PyObject_HEAD
41 
42  /* The inferior we represent. */
43  struct inferior *inferior;
44 
45  /* thread_object instances under this inferior. This list owns a
46  reference to each object it contains. */
48 
49  /* Number of threads in the list. */
50  int nthreads;
52 
53 extern PyTypeObject inferior_object_type
54  CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("inferior_object");
55 
56 static const struct inferior_data *infpy_inf_data_key;
57 
58 typedef struct {
59  PyObject_HEAD
60  void *buffer;
61 
62  /* These are kept just for mbpy_str. */
66 
67 extern PyTypeObject membuf_object_type
68  CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("membuf_object");
69 
70 /* Require that INFERIOR be a valid inferior ID. */
71 #define INFPY_REQUIRE_VALID(Inferior) \
72  do { \
73  if (!Inferior->inferior) \
74  { \
75  PyErr_SetString (PyExc_RuntimeError, \
76  _("Inferior no longer exists.")); \
77  return NULL; \
78  } \
79  } while (0)
80 
81 static void
83 {
84  struct cleanup *cleanup;
85  enum gdb_signal stop_signal;
86 
88  return;
89 
91  return;
92 
93  stop_signal = inferior_thread ()->suspend.stop_signal;
94 
96 
97  if (emit_stop_event (bs, stop_signal) < 0)
99 
100  do_cleanups (cleanup);
101 }
102 
103 static void
105 {
106  struct cleanup *cleanup;
107 
109  return;
110 
112 
113  if (emit_continue_event (ptid) < 0)
115 
116  do_cleanups (cleanup);
117 }
118 
119 /* Callback, registered as an observer, that notifies Python listeners
120  when an inferior function call is about to be made. */
121 
122 static void
124 {
125  struct cleanup *cleanup;
126 
128 
129  if (emit_inferior_call_event (INFERIOR_CALL_PRE, thread, address) < 0)
131 
132  do_cleanups (cleanup);
133 }
134 
135 /* Callback, registered as an observer, that notifies Python listeners
136  when an inferior function call has completed. */
137 
138 static void
140 {
141  struct cleanup *cleanup;
142 
144 
145  if (emit_inferior_call_event (INFERIOR_CALL_POST, thread, address) < 0)
147 
148  do_cleanups (cleanup);
149 }
150 
151 /* Callback, registered as an observer, that notifies Python listeners
152  when a part of memory has been modified by user action (eg via a
153  'set' command). */
154 
155 static void
156 python_on_memory_change (struct inferior *inferior, CORE_ADDR addr, ssize_t len, const bfd_byte *data)
157 {
158  struct cleanup *cleanup;
159 
161 
162  if (emit_memory_changed_event (addr, len) < 0)
164 
165  do_cleanups (cleanup);
166 }
167 
168 /* Callback, registered as an observer, that notifies Python listeners
169  when a register has been modified by user action (eg via a 'set'
170  command). */
171 
172 static void
174 {
175  struct cleanup *cleanup;
176 
178 
179  if (emit_register_changed_event (frame, regnum) < 0)
181 
182  do_cleanups (cleanup);
183 }
184 
185 static void
187 {
188  struct cleanup *cleanup;
189  const LONGEST *exit_code = NULL;
190 
192  return;
193 
195 
196  if (inf->has_exit_code)
197  exit_code = &inf->exit_code;
198 
199  if (emit_exited_event (exit_code, inf) < 0)
201 
202  do_cleanups (cleanup);
203 }
204 
205 /* Callback used to notify Python listeners about new objfiles loaded in the
206  inferior. OBJFILE may be NULL which means that the objfile list has been
207  cleared (emptied). */
208 
209 static void
211 {
212  struct cleanup *cleanup;
213 
215  return;
216 
217  cleanup = ensure_python_env (objfile != NULL
218  ? get_objfile_arch (objfile)
219  : target_gdbarch (),
221 
222  if (objfile == NULL)
223  {
224  if (emit_clear_objfiles_event () < 0)
226  }
227  else
228  {
229  if (emit_new_objfile_event (objfile) < 0)
231  }
232 
233  do_cleanups (cleanup);
234 }
235 
236 /* Return a reference to the Python object of type Inferior
237  representing INFERIOR. If the object has already been created,
238  return it and increment the reference count, otherwise, create it.
239  Return NULL on failure. */
240 PyObject *
242 {
243  inferior_object *inf_obj;
244 
245  inf_obj = inferior_data (inferior, infpy_inf_data_key);
246  if (!inf_obj)
247  {
248  inf_obj = PyObject_New (inferior_object, &inferior_object_type);
249  if (!inf_obj)
250  return NULL;
251 
252  inf_obj->inferior = inferior;
253  inf_obj->threads = NULL;
254  inf_obj->nthreads = 0;
255 
256  set_inferior_data (inferior, infpy_inf_data_key, inf_obj);
257 
258  }
259  else
260  Py_INCREF ((PyObject *)inf_obj);
261 
262  return (PyObject *) inf_obj;
263 }
264 
265 /* Finds the Python Inferior object for the given PID. Returns a
266  reference, or NULL if PID does not match any inferior object. */
267 
268 PyObject *
270 {
271  struct inferior *inf = find_inferior_pid (pid);
272 
273  if (inf)
274  return inferior_to_inferior_object (inf);
275 
276  return NULL;
277 }
278 
281 {
282  int pid;
283  struct threadlist_entry *thread;
284  PyObject *inf_obj;
285  thread_object *found = NULL;
286 
287  pid = ptid_get_pid (ptid);
288  if (pid == 0)
289  return NULL;
290 
291  inf_obj = find_inferior_object (pid);
292 
293  if (! inf_obj)
294  return NULL;
295 
296  for (thread = ((inferior_object *)inf_obj)->threads; thread;
297  thread = thread->next)
298  if (ptid_equal (thread->thread_obj->thread->ptid, ptid))
299  {
300  found = thread->thread_obj;
301  break;
302  }
303 
304  Py_DECREF (inf_obj);
305 
306  if (found)
307  return found;
308 
309  return NULL;
310 }
311 
312 static void
314 {
315  struct cleanup *cleanup;
316  thread_object *thread_obj;
317  inferior_object *inf_obj;
318  struct threadlist_entry *entry;
319 
321  return;
322 
324 
325  thread_obj = create_thread_object (tp);
326  if (!thread_obj)
327  {
329  do_cleanups (cleanup);
330  return;
331  }
332 
333  inf_obj = (inferior_object *) thread_obj->inf_obj;
334 
335  entry = xmalloc (sizeof (struct threadlist_entry));
336  entry->thread_obj = thread_obj;
337  entry->next = inf_obj->threads;
338 
339  inf_obj->threads = entry;
340  inf_obj->nthreads++;
341 
342  do_cleanups (cleanup);
343 }
344 
345 static void
347 {
348  struct cleanup *cleanup;
349  inferior_object *inf_obj;
350  struct threadlist_entry **entry, *tmp;
351 
353  return;
354 
356 
357  inf_obj
359  if (!inf_obj)
360  {
361  do_cleanups (cleanup);
362  return;
363  }
364 
365  /* Find thread entry in its inferior's thread_list. */
366  for (entry = &inf_obj->threads; *entry != NULL; entry =
367  &(*entry)->next)
368  if ((*entry)->thread_obj->thread == tp)
369  break;
370 
371  if (!*entry)
372  {
373  Py_DECREF (inf_obj);
374  do_cleanups (cleanup);
375  return;
376  }
377 
378  tmp = *entry;
379  tmp->thread_obj->thread = NULL;
380 
381  *entry = (*entry)->next;
382  inf_obj->nthreads--;
383 
384  Py_DECREF (tmp->thread_obj);
385  Py_DECREF (inf_obj);
386  xfree (tmp);
387 
388  do_cleanups (cleanup);
389 }
390 
391 static PyObject *
392 infpy_threads (PyObject *self, PyObject *args)
393 {
394  int i;
395  struct threadlist_entry *entry;
396  inferior_object *inf_obj = (inferior_object *) self;
397  PyObject *tuple;
398 
399  INFPY_REQUIRE_VALID (inf_obj);
400 
401  TRY
402  {
404  }
405  CATCH (except, RETURN_MASK_ALL)
406  {
407  GDB_PY_HANDLE_EXCEPTION (except);
408  }
409  END_CATCH
410 
411  tuple = PyTuple_New (inf_obj->nthreads);
412  if (!tuple)
413  return NULL;
414 
415  for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads;
416  i++, entry = entry->next)
417  {
418  Py_INCREF (entry->thread_obj);
419  PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj);
420  }
421 
422  return tuple;
423 }
424 
425 static PyObject *
426 infpy_get_num (PyObject *self, void *closure)
427 {
428  inferior_object *inf = (inferior_object *) self;
429 
430  INFPY_REQUIRE_VALID (inf);
431 
432  return PyLong_FromLong (inf->inferior->num);
433 }
434 
435 static PyObject *
436 infpy_get_pid (PyObject *self, void *closure)
437 {
438  inferior_object *inf = (inferior_object *) self;
439 
440  INFPY_REQUIRE_VALID (inf);
441 
442  return PyLong_FromLong (inf->inferior->pid);
443 }
444 
445 static PyObject *
446 infpy_get_was_attached (PyObject *self, void *closure)
447 {
448  inferior_object *inf = (inferior_object *) self;
449 
450  INFPY_REQUIRE_VALID (inf);
451  if (inf->inferior->attach_flag)
452  Py_RETURN_TRUE;
453  Py_RETURN_FALSE;
454 }
455 
456 static int
457 build_inferior_list (struct inferior *inf, void *arg)
458 {
459  PyObject *list = arg;
460  PyObject *inferior = inferior_to_inferior_object (inf);
461  int success = 0;
462 
463  if (! inferior)
464  return 0;
465 
466  success = PyList_Append (list, inferior);
467  Py_DECREF (inferior);
468 
469  if (success)
470  return 1;
471 
472  return 0;
473 }
474 
475 /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...).
476  Returns a tuple of all inferiors. */
477 PyObject *
478 gdbpy_inferiors (PyObject *unused, PyObject *unused2)
479 {
480  PyObject *list, *tuple;
481 
482  list = PyList_New (0);
483  if (!list)
484  return NULL;
485 
487  {
488  Py_DECREF (list);
489  return NULL;
490  }
491 
492  tuple = PyList_AsTuple (list);
493  Py_DECREF (list);
494 
495  return tuple;
496 }
497 
498 /* Membuf and memory manipulation. */
499 
500 /* Implementation of Inferior.read_memory (address, length).
501  Returns a Python buffer object with LENGTH bytes of the inferior's
502  memory at ADDRESS. Both arguments are integers. Returns NULL on error,
503  with a python exception set. */
504 static PyObject *
505 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
506 {
507  CORE_ADDR addr, length;
508  void *buffer = NULL;
509  membuf_object *membuf_obj;
510  PyObject *addr_obj, *length_obj, *result;
511  static char *keywords[] = { "address", "length", NULL };
512 
513  if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
514  &addr_obj, &length_obj))
515  return NULL;
516 
517  if (get_addr_from_python (addr_obj, &addr) < 0
518  || get_addr_from_python (length_obj, &length) < 0)
519  return NULL;
520 
521  TRY
522  {
523  buffer = xmalloc (length);
524 
525  read_memory (addr, buffer, length);
526  }
527  CATCH (except, RETURN_MASK_ALL)
528  {
529  xfree (buffer);
530  GDB_PY_HANDLE_EXCEPTION (except);
531  }
532  END_CATCH
533 
534  membuf_obj = PyObject_New (membuf_object, &membuf_object_type);
535  if (membuf_obj == NULL)
536  {
537  xfree (buffer);
538  return NULL;
539  }
540 
541  membuf_obj->buffer = buffer;
542  membuf_obj->addr = addr;
543  membuf_obj->length = length;
544 
545 #ifdef IS_PY3K
546  result = PyMemoryView_FromObject ((PyObject *) membuf_obj);
547 #else
548  result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
549  Py_END_OF_BUFFER);
550 #endif
551  Py_DECREF (membuf_obj);
552 
553  return result;
554 }
555 
556 /* Implementation of Inferior.write_memory (address, buffer [, length]).
557  Writes the contents of BUFFER (a Python object supporting the read
558  buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH
559  bytes from BUFFER, or its entire contents if the argument is not
560  provided. The function returns nothing. Returns NULL on error, with
561  a python exception set. */
562 static PyObject *
563 infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
564 {
565  struct gdb_exception except = exception_none;
566  Py_ssize_t buf_len;
567  const char *buffer;
568  CORE_ADDR addr, length;
569  PyObject *addr_obj, *length_obj = NULL;
570  static char *keywords[] = { "address", "buffer", "length", NULL };
571 #ifdef IS_PY3K
572  Py_buffer pybuf;
573 
574  if (! PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords,
575  &addr_obj, &pybuf,
576  &length_obj))
577  return NULL;
578 
579  buffer = pybuf.buf;
580  buf_len = pybuf.len;
581 #else
582  if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords,
583  &addr_obj, &buffer, &buf_len,
584  &length_obj))
585  return NULL;
586 #endif
587 
588  if (get_addr_from_python (addr_obj, &addr) < 0)
589  goto fail;
590 
591  if (!length_obj)
592  length = buf_len;
593  else if (get_addr_from_python (length_obj, &length) < 0)
594  goto fail;
595 
596  TRY
597  {
598  write_memory_with_notification (addr, (gdb_byte *) buffer, length);
599  }
600  CATCH (ex, RETURN_MASK_ALL)
601  {
602  except = ex;
603  }
604  END_CATCH
605 
606 #ifdef IS_PY3K
607  PyBuffer_Release (&pybuf);
608 #endif
609  GDB_PY_HANDLE_EXCEPTION (except);
610 
611  Py_RETURN_NONE;
612 
613  fail:
614 #ifdef IS_PY3K
615  PyBuffer_Release (&pybuf);
616 #endif
617  return NULL;
618 }
619 
620 /* Destructor of Membuf objects. */
621 static void
622 mbpy_dealloc (PyObject *self)
623 {
624  xfree (((membuf_object *) self)->buffer);
625  Py_TYPE (self)->tp_free (self);
626 }
627 
628 /* Return a description of the Membuf object. */
629 static PyObject *
630 mbpy_str (PyObject *self)
631 {
632  membuf_object *membuf_obj = (membuf_object *) self;
633 
634  return PyString_FromFormat (_("Memory buffer for address %s, \
635 which is %s bytes long."),
636  paddress (python_gdbarch, membuf_obj->addr),
637  pulongest (membuf_obj->length));
638 }
639 
640 #ifdef IS_PY3K
641 
642 static int
643 get_buffer (PyObject *self, Py_buffer *buf, int flags)
644 {
645  membuf_object *membuf_obj = (membuf_object *) self;
646  int ret;
647 
648  ret = PyBuffer_FillInfo (buf, self, membuf_obj->buffer,
649  membuf_obj->length, 0,
650  PyBUF_CONTIG);
651  buf->format = "c";
652 
653  return ret;
654 }
655 
656 #else
657 
658 static Py_ssize_t
659 get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
660 {
661  membuf_object *membuf_obj = (membuf_object *) self;
662 
663  if (segment)
664  {
665  PyErr_SetString (PyExc_SystemError,
666  _("The memory buffer supports only one segment."));
667  return -1;
668  }
669 
670  *ptrptr = membuf_obj->buffer;
671 
672  return membuf_obj->length;
673 }
674 
675 static Py_ssize_t
676 get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
677 {
678  return get_read_buffer (self, segment, ptrptr);
679 }
680 
681 static Py_ssize_t
682 get_seg_count (PyObject *self, Py_ssize_t *lenp)
683 {
684  if (lenp)
685  *lenp = ((membuf_object *) self)->length;
686 
687  return 1;
688 }
689 
690 static Py_ssize_t
691 get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
692 {
693  void *ptr = NULL;
694  Py_ssize_t ret;
695 
696  ret = get_read_buffer (self, segment, &ptr);
697  *ptrptr = (char *) ptr;
698 
699  return ret;
700 }
701 
702 #endif /* IS_PY3K */
703 
704 /* Implementation of
705  gdb.search_memory (address, length, pattern). ADDRESS is the
706  address to start the search. LENGTH specifies the scope of the
707  search from ADDRESS. PATTERN is the pattern to search for (and
708  must be a Python object supporting the buffer protocol).
709  Returns a Python Long object holding the address where the pattern
710  was located, or if the pattern was not found, returns None. Returns NULL
711  on error, with a python exception set. */
712 static PyObject *
713 infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
714 {
715  struct gdb_exception except = exception_none;
716  CORE_ADDR start_addr, length;
717  static char *keywords[] = { "address", "length", "pattern", NULL };
718  PyObject *start_addr_obj, *length_obj;
719  Py_ssize_t pattern_size;
720  const void *buffer;
721  CORE_ADDR found_addr;
722  int found = 0;
723 #ifdef IS_PY3K
724  Py_buffer pybuf;
725 
726  if (! PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords,
727  &start_addr_obj, &length_obj,
728  &pybuf))
729  return NULL;
730 
731  buffer = pybuf.buf;
732  pattern_size = pybuf.len;
733 #else
734  PyObject *pattern;
735 
736  if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
737  &start_addr_obj, &length_obj,
738  &pattern))
739  return NULL;
740 
741  if (!PyObject_CheckReadBuffer (pattern))
742  {
743  PyErr_SetString (PyExc_RuntimeError,
744  _("The pattern is not a Python buffer."));
745 
746  return NULL;
747  }
748 
749  if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1)
750  return NULL;
751 #endif
752 
753  if (get_addr_from_python (start_addr_obj, &start_addr) < 0)
754  goto fail;
755 
756  if (get_addr_from_python (length_obj, &length) < 0)
757  goto fail;
758 
759  if (!length)
760  {
761  PyErr_SetString (PyExc_ValueError,
762  _("Search range is empty."));
763  goto fail;
764  }
765  /* Watch for overflows. */
766  else if (length > CORE_ADDR_MAX
767  || (start_addr + length - 1) < start_addr)
768  {
769  PyErr_SetString (PyExc_ValueError,
770  _("The search range is too large."));
771  goto fail;
772  }
773 
774  TRY
775  {
776  found = target_search_memory (start_addr, length,
777  buffer, pattern_size,
778  &found_addr);
779  }
780  CATCH (ex, RETURN_MASK_ALL)
781  {
782  except = ex;
783  }
784  END_CATCH
785 
786 #ifdef IS_PY3K
787  PyBuffer_Release (&pybuf);
788 #endif
789  GDB_PY_HANDLE_EXCEPTION (except);
790 
791  if (found)
792  return PyLong_FromLong (found_addr);
793  else
794  Py_RETURN_NONE;
795 
796  fail:
797 #ifdef IS_PY3K
798  PyBuffer_Release (&pybuf);
799 #endif
800  return NULL;
801 }
802 
803 /* Implementation of gdb.Inferior.is_valid (self) -> Boolean.
804  Returns True if this inferior object still exists in GDB. */
805 
806 static PyObject *
807 infpy_is_valid (PyObject *self, PyObject *args)
808 {
809  inferior_object *inf = (inferior_object *) self;
810 
811  if (! inf->inferior)
812  Py_RETURN_FALSE;
813 
814  Py_RETURN_TRUE;
815 }
816 
817 static void
818 infpy_dealloc (PyObject *obj)
819 {
820  inferior_object *inf_obj = (inferior_object *) obj;
821  struct inferior *inf = inf_obj->inferior;
822 
823  if (! inf)
824  return;
825 
826  set_inferior_data (inf, infpy_inf_data_key, NULL);
827 }
828 
829 /* Clear the INFERIOR pointer in an Inferior object and clear the
830  thread list. */
831 static void
832 py_free_inferior (struct inferior *inf, void *datum)
833 {
834 
835  struct cleanup *cleanup;
836  inferior_object *inf_obj = datum;
837  struct threadlist_entry *th_entry, *th_tmp;
838 
840  return;
841 
843 
844  inf_obj->inferior = NULL;
845 
846  /* Deallocate threads list. */
847  for (th_entry = inf_obj->threads; th_entry != NULL;)
848  {
849  Py_DECREF (th_entry->thread_obj);
850 
851  th_tmp = th_entry;
852  th_entry = th_entry->next;
853  xfree (th_tmp);
854  }
855 
856  inf_obj->nthreads = 0;
857 
858  Py_DECREF ((PyObject *) inf_obj);
859  do_cleanups (cleanup);
860 }
861 
862 /* Implementation of gdb.selected_inferior() -> gdb.Inferior.
863  Returns the current inferior object. */
864 
865 PyObject *
866 gdbpy_selected_inferior (PyObject *self, PyObject *args)
867 {
869 }
870 
871 int
873 {
874  if (PyType_Ready (&inferior_object_type) < 0)
875  return -1;
876 
877  if (gdb_pymodule_addobject (gdb_module, "Inferior",
878  (PyObject *) &inferior_object_type) < 0)
879  return -1;
880 
881  infpy_inf_data_key =
882  register_inferior_data_with_cleanup (NULL, py_free_inferior);
883 
894 
895  membuf_object_type.tp_new = PyType_GenericNew;
896  if (PyType_Ready (&membuf_object_type) < 0)
897  return -1;
898 
899  return gdb_pymodule_addobject (gdb_module, "Membuf", (PyObject *)
900  &membuf_object_type);
901 }
902 
903 static PyGetSetDef inferior_object_getset[] =
904 {
905  { "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL },
906  { "pid", infpy_get_pid, NULL, "PID of inferior, as assigned by the OS.",
907  NULL },
908  { "was_attached", infpy_get_was_attached, NULL,
909  "True if the inferior was created using 'attach'.", NULL },
910  { NULL }
911 };
912 
913 static PyMethodDef inferior_object_methods[] =
914 {
915  { "is_valid", infpy_is_valid, METH_NOARGS,
916  "is_valid () -> Boolean.\n\
917 Return true if this inferior is valid, false if not." },
918  { "threads", infpy_threads, METH_NOARGS,
919  "Return all the threads of this inferior." },
920  { "read_memory", (PyCFunction) infpy_read_memory,
921  METH_VARARGS | METH_KEYWORDS,
922  "read_memory (address, length) -> buffer\n\
923 Return a buffer object for reading from the inferior's memory." },
924  { "write_memory", (PyCFunction) infpy_write_memory,
925  METH_VARARGS | METH_KEYWORDS,
926  "write_memory (address, buffer [, length])\n\
927 Write the given buffer object to the inferior's memory." },
928  { "search_memory", (PyCFunction) infpy_search_memory,
929  METH_VARARGS | METH_KEYWORDS,
930  "search_memory (address, length, pattern) -> long\n\
931 Return a long with the address of a match, or None." },
932  { NULL }
933 };
934 
935 PyTypeObject inferior_object_type =
936 {
937  PyVarObject_HEAD_INIT (NULL, 0)
938  "gdb.Inferior", /* tp_name */
939  sizeof (inferior_object), /* tp_basicsize */
940  0, /* tp_itemsize */
941  infpy_dealloc, /* tp_dealloc */
942  0, /* tp_print */
943  0, /* tp_getattr */
944  0, /* tp_setattr */
945  0, /* tp_compare */
946  0, /* tp_repr */
947  0, /* tp_as_number */
948  0, /* tp_as_sequence */
949  0, /* tp_as_mapping */
950  0, /* tp_hash */
951  0, /* tp_call */
952  0, /* tp_str */
953  0, /* tp_getattro */
954  0, /* tp_setattro */
955  0, /* tp_as_buffer */
956  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /* tp_flags */
957  "GDB inferior object", /* tp_doc */
958  0, /* tp_traverse */
959  0, /* tp_clear */
960  0, /* tp_richcompare */
961  0, /* tp_weaklistoffset */
962  0, /* tp_iter */
963  0, /* tp_iternext */
964  inferior_object_methods, /* tp_methods */
965  0, /* tp_members */
966  inferior_object_getset, /* tp_getset */
967  0, /* tp_base */
968  0, /* tp_dict */
969  0, /* tp_descr_get */
970  0, /* tp_descr_set */
971  0, /* tp_dictoffset */
972  0, /* tp_init */
973  0 /* tp_alloc */
974 };
975 
976 #ifdef IS_PY3K
977 
978 static PyBufferProcs buffer_procs =
979 {
980  get_buffer
981 };
982 
983 #else
984 
985 /* Python doesn't provide a decent way to get compatibility here. */
986 #if HAVE_LIBPYTHON2_4
987 #define CHARBUFFERPROC_NAME getcharbufferproc
988 #else
989 #define CHARBUFFERPROC_NAME charbufferproc
990 #endif
991 
992 static PyBufferProcs buffer_procs = {
996  /* The cast here works around a difference between Python 2.4 and
997  Python 2.5. */
999 };
1000 #endif /* IS_PY3K */
1001 
1002 PyTypeObject membuf_object_type = {
1003  PyVarObject_HEAD_INIT (NULL, 0)
1004  "gdb.Membuf", /*tp_name*/
1005  sizeof (membuf_object), /*tp_basicsize*/
1006  0, /*tp_itemsize*/
1007  mbpy_dealloc, /*tp_dealloc*/
1008  0, /*tp_print*/
1009  0, /*tp_getattr*/
1010  0, /*tp_setattr*/
1011  0, /*tp_compare*/
1012  0, /*tp_repr*/
1013  0, /*tp_as_number*/
1014  0, /*tp_as_sequence*/
1015  0, /*tp_as_mapping*/
1016  0, /*tp_hash */
1017  0, /*tp_call*/
1018  mbpy_str, /*tp_str*/
1019  0, /*tp_getattro*/
1020  0, /*tp_setattro*/
1021  &buffer_procs, /*tp_as_buffer*/
1022  Py_TPFLAGS_DEFAULT, /*tp_flags*/
1023  "GDB memory buffer object", /*tp_doc*/
1024  0, /* tp_traverse */
1025  0, /* tp_clear */
1026  0, /* tp_richcompare */
1027  0, /* tp_weaklistoffset */
1028  0, /* tp_iter */
1029  0, /* tp_iternext */
1030  0, /* tp_methods */
1031  0, /* tp_members */
1032  0, /* tp_getset */
1033  0, /* tp_base */
1034  0, /* tp_dict */
1035  0, /* tp_descr_get */
1036  0, /* tp_descr_set */
1037  0, /* tp_dictoffset */
1038  0, /* tp_init */
1039  0, /* tp_alloc */
1040 };
struct gdbarch * target_gdbarch(void)
Definition: gdbarch.c:5143
static void python_on_resume(ptid_t ptid)
Definition: py-inferior.c:104
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition: py-utils.c:437
#define Py_DECREF(op)
static PyObject * infpy_is_valid(PyObject *self, PyObject *args)
Definition: py-inferior.c:807
struct thread_info * find_thread_ptid(ptid_t ptid)
Definition: thread.c:393
#define INFPY_REQUIRE_VALID(Inferior)
Definition: py-inferior.c:71
struct observer * observer_attach_thread_exit(observer_thread_exit_ftype *f)
static void python_on_normal_stop(struct bpstats *bs, int print_frame)
Definition: py-inferior.c:82
static void add_thread_object(struct thread_info *tp)
Definition: py-inferior.c:313
int ptid_equal(ptid_t ptid1, ptid_t ptid2)
Definition: ptid.c:76
bfd_vma CORE_ADDR
Definition: common-types.h:41
void xfree(void *)
Definition: common-utils.c:97
thread_object * create_thread_object(struct thread_info *tp)
Definition: py-infthread.c:40
static void mbpy_dealloc(PyObject *self)
Definition: py-inferior.c:622
Definition: py-inferior.c:33
int pid
Definition: inferior.h:299
static PyObject * infpy_threads(PyObject *self, PyObject *args)
Definition: py-inferior.c:392
int num
Definition: inferior.h:295
struct thread_info * inferior_thread(void)
Definition: thread.c:85
const struct gdb_exception exception_none
#define CHARBUFFERPROC_NAME
Definition: py-inferior.c:989
struct observer * observer_attach_inferior_exit(observer_inferior_exit_ftype *f)
int emit_clear_objfiles_event(void)
struct inferior * iterate_over_inferiors(int(*callback)(struct inferior *, void *), void *data)
Definition: inferior.c:398
LONGEST exit_code
Definition: inferior.h:375
int emit_stop_event(struct bpstats *bs, enum gdb_signal stop_signal)
Definition: py-stopevent.c:46
#define _(String)
Definition: gdb_locale.h:40
enum gdb_signal stop_signal
Definition: gdbthread.h:159
#define END_CATCH
int emit_inferior_call_event(inferior_call_kind kind, ptid_t thread, CORE_ADDR addr)
Definition: py-infevents.c:188
static PyObject * infpy_get_was_attached(PyObject *self, void *closure)
Definition: py-inferior.c:446
PyObject * inf_obj
static void python_on_inferior_call_post(ptid_t thread, CORE_ADDR address)
Definition: py-inferior.c:139
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: utils.c:2743
Definition: ptid.h:35
int gdbpy_initialize_inferior(void)
Definition: py-inferior.c:872
static void py_free_inferior(struct inferior *inf, void *datum)
Definition: py-inferior.c:832
#define GDB_PY_HANDLE_EXCEPTION(Exception)
mach_port_t kern_return_t mach_port_t msgports mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition: gnu-nat.c:1885
static void infpy_dealloc(PyObject *obj)
Definition: py-inferior.c:818
#define TRY
static int build_inferior_list(struct inferior *inf, void *arg)
Definition: py-inferior.c:457
#define CATCH(EXCEPTION, MASK)
static const struct inferior_data * infpy_inf_data_key
Definition: py-inferior.c:56
PyObject_HEAD struct inferior * inferior
Definition: py-inferior.c:43
static Py_ssize_t get_read_buffer(PyObject *self, Py_ssize_t segment, void **ptrptr)
Definition: py-inferior.c:659
#define CORE_ADDR_MAX
Definition: common-types.h:59
static PyObject * infpy_get_pid(PyObject *self, void *closure)
Definition: py-inferior.c:436
int emit_exited_event(const LONGEST *exit_code, struct inferior *inf)
PyObject * gdbpy_selected_inferior(PyObject *self, PyObject *args)
Definition: py-inferior.c:866
thread_object * find_thread_object(ptid_t ptid)
Definition: py-inferior.c:280
int emit_memory_changed_event(CORE_ADDR addr, ssize_t len)
Definition: py-infevents.c:206
struct gdbarch * get_objfile_arch(const struct objfile *objfile)
Definition: objfiles.c:368
struct inferior * find_inferior_pid(int pid)
Definition: inferior.c:354
Definition: gnu-nat.c:163
struct gdbarch * get_current_arch(void)
Definition: arch-utils.c:781
struct gdbarch * python_gdbarch
Definition: python.c:201
struct cleanup * ensure_python_env(struct gdbarch *gdbarch, const struct language_defn *language)
Definition: python.c:247
static PyObject * infpy_read_memory(PyObject *self, PyObject *args, PyObject *kw)
Definition: py-inferior.c:505
static PyObject * infpy_search_memory(PyObject *self, PyObject *args, PyObject *kw)
Definition: py-inferior.c:713
struct observer * observer_attach_target_resumed(observer_target_resumed_ftype *f)
int gdb_python_initialized
Definition: python.c:104
int emit_register_changed_event(struct frame_info *frame, int regnum)
Definition: py-infevents.c:223
static PyMethodDef inferior_object_methods[]
Definition: py-inferior.c:913
int regnum
Definition: aarch64-tdep.c:69
void read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: corefile.c:244
void * xmalloc(YYSIZE_T)
static void python_on_inferior_call_pre(ptid_t thread, CORE_ADDR address)
Definition: py-inferior.c:123
PyTypeObject inferior_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("inferior_object")
int attach_flag
Definition: inferior.h:340
int emit_new_objfile_event(struct objfile *objfile)
PyObject_HEAD struct thread_info * thread
int ptid_get_pid(ptid_t ptid)
Definition: ptid.c:52
static PyGetSetDef inferior_object_getset[]
Definition: py-inferior.c:903
static void python_on_memory_change(struct inferior *inferior, CORE_ADDR addr, ssize_t len, const bfd_byte *data)
Definition: py-inferior.c:156
bfd_byte gdb_byte
Definition: common-types.h:38
static void print_frame(struct frame_info *frame, int print_level, enum print_what print_what, int print_args, struct symtab_and_line sal)
const struct language_defn * current_language
Definition: language.c:85
struct threadlist_entry * next
Definition: py-inferior.c:35
void update_thread_list(void)
Definition: thread.c:1735
ptid_t ptid
Definition: gdbthread.h:169
PyObject * find_inferior_object(int pid)
Definition: py-inferior.c:269
static Py_ssize_t get_seg_count(PyObject *self, Py_ssize_t *lenp)
Definition: py-inferior.c:682
struct observer * observer_attach_register_changed(observer_register_changed_ftype *f)
ptid_t inferior_ptid
Definition: infcmd.c:124
int target_search_memory(CORE_ADDR start_addr, ULONGEST search_space_len, const gdb_byte *pattern, ULONGEST pattern_len, CORE_ADDR *found_addrp)
Definition: target.c:2448
static PyObject * infpy_write_memory(PyObject *self, PyObject *args, PyObject *kw)
Definition: py-inferior.c:563
struct observer * observer_attach_inferior_call_pre(observer_inferior_call_pre_ftype *f)
void gdbpy_print_stack(void)
Definition: python.c:1199
Definition: buffer.h:23
struct thread_suspend_state suspend
Definition: gdbthread.h:202
CORE_ADDR addr
Definition: py-inferior.c:63
static Py_ssize_t get_char_buffer(PyObject *self, Py_ssize_t segment, char **ptrptr)
Definition: py-inferior.c:691
int emit_continue_event(ptid_t ptid)
struct inferior * current_inferior(void)
Definition: inferior.c:57
static void python_on_register_change(struct frame_info *frame, int regnum)
Definition: py-inferior.c:173
struct observer * observer_attach_inferior_call_post(observer_inferior_call_post_ftype *f)
static int ignore(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: corelow.c:917
int has_exit_code
Definition: inferior.h:374
static void python_new_objfile(struct objfile *objfile)
Definition: py-inferior.c:210
struct observer * observer_attach_normal_stop(observer_normal_stop_ftype *f)
struct observer * observer_attach_new_objfile(observer_new_objfile_ftype *f)
int get_addr_from_python(PyObject *obj, CORE_ADDR *addr)
Definition: py-utils.c:315
PyObject_HEAD void * buffer
Definition: py-inferior.c:60
struct observer * observer_attach_new_thread(observer_new_thread_ftype *f)
static PyObject * mbpy_str(PyObject *self)
Definition: py-inferior.c:630
PyObject * gdb_module
Definition: python.c:112
thread_object * thread_obj
Definition: py-inferior.c:34
const struct language_defn * python_language
Definition: python.c:202
static PyObject * infpy_get_num(PyObject *self, void *closure)
Definition: py-inferior.c:426
CORE_ADDR length
Definition: py-inferior.c:64
static Py_ssize_t get_write_buffer(PyObject *self, Py_ssize_t segment, void **ptrptr)
Definition: py-inferior.c:676
void write_memory_with_notification(CORE_ADDR memaddr, const bfd_byte *myaddr, ssize_t len)
Definition: corefile.c:402
PyObject * inferior_to_inferior_object(struct inferior *inferior)
Definition: py-inferior.c:241
PyObject * gdbpy_inferiors(PyObject *unused, PyObject *unused2)
Definition: py-inferior.c:478
struct observer * observer_attach_memory_changed(observer_memory_changed_ftype *f)
static void python_inferior_exit(struct inferior *inf)
Definition: py-inferior.c:186
struct threadlist_entry * threads
Definition: py-inferior.c:47
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1818
#define Py_TYPE(ob)
long long LONGEST
Definition: common-types.h:52
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175
static void delete_thread_object(struct thread_info *tp, int ignore)
Definition: py-inferior.c:346
const ULONGEST const LONGEST len
Definition: target.h:309
#define PyVarObject_HEAD_INIT(type, size)