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