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