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