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