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