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