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