]>
Commit | Line | Data |
---|---|---|
595939de PM |
1 | /* Python interface to inferiors. |
2 | ||
7b6bb8da | 3 | Copyright (C) 2009, 2010, 2011 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" | |
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 | |
33 | struct threadlist_entry { | |
34 | thread_object *thread_obj; | |
35 | struct threadlist_entry *next; | |
36 | }; | |
37 | ||
38 | typedef 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 | ||
53 | static PyTypeObject inferior_object_type; | |
54 | ||
55 | static const struct inferior_data *infpy_inf_data_key; | |
56 | ||
57 | typedef struct { | |
58 | PyObject_HEAD | |
59 | void *buffer; | |
60 | ||
61 | /* These are kept just for mbpy_str. */ | |
62 | CORE_ADDR addr; | |
63 | CORE_ADDR length; | |
64 | } membuf_object; | |
65 | ||
66 | static PyTypeObject membuf_object_type; | |
67 | ||
68 | /* Require that INFERIOR be a valid inferior ID. */ | |
69 | #define INFPY_REQUIRE_VALID(Inferior) \ | |
70 | do { \ | |
71 | if (!Inferior->inferior) \ | |
72 | { \ | |
73 | PyErr_SetString (PyExc_RuntimeError, \ | |
74 | _("Inferior no longer exists.")); \ | |
75 | return NULL; \ | |
76 | } \ | |
77 | } while (0) | |
78 | ||
505500db SW |
79 | static void |
80 | python_on_normal_stop (struct bpstats *bs, int print_frame) | |
81 | { | |
82 | struct cleanup *cleanup; | |
83 | enum target_signal stop_signal; | |
84 | ||
85 | if (!find_thread_ptid (inferior_ptid)) | |
86 | return; | |
87 | ||
88 | stop_signal = inferior_thread ()->suspend.stop_signal; | |
89 | ||
90 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
91 | ||
92 | if (emit_stop_event (bs, stop_signal) < 0) | |
93 | gdbpy_print_stack (); | |
94 | ||
95 | do_cleanups (cleanup); | |
96 | } | |
97 | ||
98 | static void | |
99 | python_on_resume (ptid_t ptid) | |
100 | { | |
101 | struct cleanup *cleanup; | |
102 | ||
103 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
104 | ||
105 | if (emit_continue_event (ptid) < 0) | |
106 | gdbpy_print_stack (); | |
107 | ||
108 | do_cleanups (cleanup); | |
109 | } | |
110 | ||
111 | static void | |
112 | python_inferior_exit (struct inferior *inf) | |
113 | { | |
114 | struct cleanup *cleanup; | |
115 | LONGEST exit_code = -1; | |
116 | ptid_t ptidp; | |
117 | struct target_waitstatus status; | |
118 | ||
119 | cleanup = ensure_python_env (get_current_arch (), current_language); | |
120 | ||
121 | get_last_target_status (&ptidp, &status); | |
122 | ||
123 | exit_code = status.value.integer; | |
124 | ||
125 | if (exit_code >= 0 | |
126 | && emit_exited_event (exit_code) < 0) | |
127 | gdbpy_print_stack (); | |
128 | ||
129 | do_cleanups (cleanup); | |
130 | } | |
131 | ||
595939de PM |
132 | /* Return a borrowed reference to the Python object of type Inferior |
133 | representing INFERIOR. If the object has already been created, | |
134 | return it, otherwise, create it. Return NULL on failure. */ | |
135 | PyObject * | |
136 | inferior_to_inferior_object (struct inferior *inferior) | |
137 | { | |
138 | inferior_object *inf_obj; | |
139 | ||
140 | inf_obj = inferior_data (inferior, infpy_inf_data_key); | |
141 | if (!inf_obj) | |
142 | { | |
143 | struct cleanup *cleanup; | |
144 | cleanup = ensure_python_env (python_gdbarch, python_language); | |
145 | ||
146 | inf_obj = PyObject_New (inferior_object, &inferior_object_type); | |
147 | if (!inf_obj) | |
148 | { | |
149 | do_cleanups (cleanup); | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | inf_obj->inferior = inferior; | |
154 | inf_obj->threads = NULL; | |
155 | inf_obj->nthreads = 0; | |
156 | ||
157 | set_inferior_data (inferior, infpy_inf_data_key, inf_obj); | |
158 | ||
159 | do_cleanups (cleanup); | |
160 | } | |
161 | ||
162 | return (PyObject *) inf_obj; | |
163 | } | |
164 | ||
165 | /* Finds the Python Inferior object for the given PID. Returns a | |
166 | borrowed reference, or NULL if PID does not match any inferior | |
505500db SW |
167 | object. */ |
168 | ||
595939de PM |
169 | PyObject * |
170 | find_inferior_object (int pid) | |
171 | { | |
172 | struct inflist_entry *p; | |
173 | struct inferior *inf = find_inferior_pid (pid); | |
174 | ||
175 | if (inf) | |
176 | return inferior_to_inferior_object (inf); | |
177 | ||
178 | return NULL; | |
179 | } | |
180 | ||
181 | thread_object * | |
182 | find_thread_object (ptid_t ptid) | |
183 | { | |
184 | int pid; | |
185 | struct threadlist_entry *thread; | |
186 | PyObject *inf_obj; | |
187 | ||
188 | pid = PIDGET (ptid); | |
ea976c60 PM |
189 | if (pid == 0) |
190 | return NULL; | |
191 | ||
595939de PM |
192 | inf_obj = find_inferior_object (pid); |
193 | ||
194 | if (inf_obj) | |
195 | for (thread = ((inferior_object *)inf_obj)->threads; thread; | |
196 | thread = thread->next) | |
197 | if (ptid_equal (thread->thread_obj->thread->ptid, ptid)) | |
198 | return thread->thread_obj; | |
199 | ||
200 | return NULL; | |
201 | } | |
202 | ||
203 | static void | |
204 | add_thread_object (struct thread_info *tp) | |
205 | { | |
206 | struct cleanup *cleanup; | |
207 | thread_object *thread_obj; | |
208 | inferior_object *inf_obj; | |
209 | struct threadlist_entry *entry; | |
210 | ||
211 | cleanup = ensure_python_env (python_gdbarch, python_language); | |
212 | ||
213 | thread_obj = create_thread_object (tp); | |
214 | if (!thread_obj) | |
215 | { | |
216 | gdbpy_print_stack (); | |
217 | do_cleanups (cleanup); | |
218 | return; | |
219 | } | |
220 | ||
221 | inf_obj = (inferior_object *) thread_obj->inf_obj; | |
222 | ||
223 | entry = xmalloc (sizeof (struct threadlist_entry)); | |
224 | entry->thread_obj = thread_obj; | |
225 | entry->next = inf_obj->threads; | |
226 | ||
227 | inf_obj->threads = entry; | |
228 | inf_obj->nthreads++; | |
229 | ||
230 | do_cleanups (cleanup); | |
231 | } | |
232 | ||
233 | static void | |
234 | delete_thread_object (struct thread_info *tp, int ignore) | |
235 | { | |
236 | struct cleanup *cleanup; | |
237 | inferior_object *inf_obj; | |
238 | thread_object *thread_obj; | |
239 | struct threadlist_entry **entry, *tmp; | |
240 | ||
241 | inf_obj = (inferior_object *) find_inferior_object (PIDGET(tp->ptid)); | |
242 | if (!inf_obj) | |
243 | return; | |
244 | ||
245 | /* Find thread entry in its inferior's thread_list. */ | |
246 | for (entry = &inf_obj->threads; *entry != NULL; entry = | |
247 | &(*entry)->next) | |
248 | if ((*entry)->thread_obj->thread == tp) | |
249 | break; | |
250 | ||
251 | if (!*entry) | |
252 | return; | |
253 | ||
254 | cleanup = ensure_python_env (python_gdbarch, python_language); | |
255 | ||
256 | tmp = *entry; | |
257 | tmp->thread_obj->thread = NULL; | |
258 | ||
259 | *entry = (*entry)->next; | |
260 | inf_obj->nthreads--; | |
261 | ||
262 | Py_DECREF (tmp->thread_obj); | |
263 | xfree (tmp); | |
264 | ||
265 | do_cleanups (cleanup); | |
266 | } | |
267 | ||
268 | static PyObject * | |
269 | infpy_threads (PyObject *self, PyObject *args) | |
270 | { | |
271 | int i; | |
272 | struct threadlist_entry *entry; | |
273 | inferior_object *inf_obj = (inferior_object *) self; | |
274 | PyObject *tuple; | |
275 | ||
276 | INFPY_REQUIRE_VALID (inf_obj); | |
277 | ||
278 | tuple = PyTuple_New (inf_obj->nthreads); | |
279 | if (!tuple) | |
280 | return NULL; | |
281 | ||
282 | for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads; | |
283 | i++, entry = entry->next) | |
284 | { | |
285 | Py_INCREF (entry->thread_obj); | |
286 | PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj); | |
287 | } | |
288 | ||
289 | return tuple; | |
290 | } | |
291 | ||
292 | static PyObject * | |
293 | infpy_get_num (PyObject *self, void *closure) | |
294 | { | |
295 | inferior_object *inf = (inferior_object *) self; | |
296 | ||
297 | INFPY_REQUIRE_VALID (inf); | |
298 | ||
299 | return PyLong_FromLong (inf->inferior->num); | |
300 | } | |
301 | ||
302 | static PyObject * | |
303 | infpy_get_pid (PyObject *self, void *closure) | |
304 | { | |
305 | inferior_object *inf = (inferior_object *) self; | |
306 | ||
307 | INFPY_REQUIRE_VALID (inf); | |
308 | ||
309 | return PyLong_FromLong (inf->inferior->pid); | |
310 | } | |
311 | ||
312 | static PyObject * | |
313 | infpy_get_was_attached (PyObject *self, void *closure) | |
314 | { | |
315 | inferior_object *inf = (inferior_object *) self; | |
316 | ||
317 | INFPY_REQUIRE_VALID (inf); | |
318 | if (inf->inferior->attach_flag) | |
319 | Py_RETURN_TRUE; | |
320 | Py_RETURN_FALSE; | |
321 | } | |
322 | ||
323 | static int | |
324 | build_inferior_list (struct inferior *inf, void *arg) | |
325 | { | |
326 | PyObject *list = arg; | |
327 | PyObject *inferior = inferior_to_inferior_object (inf); | |
328 | ||
329 | PyList_Append (list, inferior); | |
330 | return 0; | |
331 | } | |
332 | ||
333 | /* Implementation of gdb.inferiors () -> (gdb.Inferior, ...). | |
334 | Returns a tuple of all inferiors. */ | |
335 | PyObject * | |
336 | gdbpy_inferiors (PyObject *unused, PyObject *unused2) | |
337 | { | |
338 | int i = 0; | |
339 | PyObject *list, *inferior; | |
340 | struct inferior *inf; | |
341 | ||
342 | list = PyList_New (0); | |
343 | if (!list) | |
344 | return NULL; | |
345 | ||
346 | iterate_over_inferiors (build_inferior_list, list); | |
347 | ||
348 | return PyList_AsTuple (list); | |
349 | } | |
350 | ||
351 | /* Membuf and memory manipulation. */ | |
352 | ||
353 | /* Implementation of gdb.read_memory (address, length). | |
354 | Returns a Python buffer object with LENGTH bytes of the inferior's | |
8dc78533 JK |
355 | memory at ADDRESS. Both arguments are integers. Returns NULL on error, |
356 | with a python exception set. */ | |
595939de PM |
357 | static PyObject * |
358 | infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) | |
359 | { | |
360 | int error = 0; | |
361 | CORE_ADDR addr, length; | |
362 | void *buffer = NULL; | |
363 | membuf_object *membuf_obj; | |
364 | PyObject *addr_obj, *length_obj; | |
365 | struct cleanup *cleanups; | |
366 | volatile struct gdb_exception except; | |
367 | static char *keywords[] = { "address", "length", NULL }; | |
368 | ||
369 | if (! PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords, | |
370 | &addr_obj, &length_obj)) | |
371 | return NULL; | |
372 | ||
373 | cleanups = make_cleanup (null_cleanup, NULL); | |
374 | ||
375 | TRY_CATCH (except, RETURN_MASK_ALL) | |
376 | { | |
377 | if (!get_addr_from_python (addr_obj, &addr) | |
378 | || !get_addr_from_python (length_obj, &length)) | |
379 | { | |
380 | error = 1; | |
381 | break; | |
382 | } | |
383 | ||
384 | buffer = xmalloc (length); | |
385 | make_cleanup (xfree, buffer); | |
386 | ||
387 | read_memory (addr, buffer, length); | |
388 | } | |
389 | if (except.reason < 0) | |
390 | { | |
391 | do_cleanups (cleanups); | |
392 | GDB_PY_HANDLE_EXCEPTION (except); | |
393 | } | |
394 | ||
395 | if (error) | |
396 | { | |
397 | do_cleanups (cleanups); | |
398 | return NULL; | |
399 | } | |
400 | ||
401 | membuf_obj = PyObject_New (membuf_object, &membuf_object_type); | |
402 | if (membuf_obj == NULL) | |
403 | { | |
404 | PyErr_SetString (PyExc_MemoryError, | |
405 | _("Could not allocate memory buffer object.")); | |
406 | do_cleanups (cleanups); | |
407 | return NULL; | |
408 | } | |
409 | ||
410 | discard_cleanups (cleanups); | |
411 | ||
412 | membuf_obj->buffer = buffer; | |
413 | membuf_obj->addr = addr; | |
414 | membuf_obj->length = length; | |
415 | ||
416 | return PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0, | |
417 | Py_END_OF_BUFFER); | |
418 | } | |
419 | ||
420 | /* Implementation of gdb.write_memory (address, buffer [, length]). | |
421 | Writes the contents of BUFFER (a Python object supporting the read | |
422 | buffer protocol) at ADDRESS in the inferior's memory. Write LENGTH | |
423 | bytes from BUFFER, or its entire contents if the argument is not | |
8dc78533 JK |
424 | provided. The function returns nothing. Returns NULL on error, with |
425 | a python exception set. */ | |
595939de PM |
426 | static PyObject * |
427 | infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) | |
428 | { | |
429 | int buf_len, error = 0; | |
430 | const char *buffer; | |
431 | CORE_ADDR addr, length; | |
432 | PyObject *addr_obj, *length_obj = NULL; | |
433 | volatile struct gdb_exception except; | |
434 | static char *keywords[] = { "address", "buffer", "length", NULL }; | |
435 | ||
436 | ||
437 | if (! PyArg_ParseTupleAndKeywords (args, kw, "Os#|O", keywords, | |
438 | &addr_obj, &buffer, &buf_len, | |
439 | &length_obj)) | |
440 | return NULL; | |
441 | ||
442 | TRY_CATCH (except, RETURN_MASK_ALL) | |
443 | { | |
444 | if (!get_addr_from_python (addr_obj, &addr)) | |
445 | { | |
446 | error = 1; | |
447 | break; | |
448 | } | |
449 | ||
450 | if (!length_obj) | |
451 | length = buf_len; | |
452 | else if (!get_addr_from_python (length_obj, &length)) | |
453 | { | |
454 | error = 1; | |
455 | break; | |
456 | } | |
457 | write_memory (addr, buffer, length); | |
458 | } | |
459 | GDB_PY_HANDLE_EXCEPTION (except); | |
460 | ||
461 | if (error) | |
462 | return NULL; | |
463 | ||
464 | Py_RETURN_NONE; | |
465 | } | |
466 | ||
467 | /* Destructor of Membuf objects. */ | |
468 | static void | |
469 | mbpy_dealloc (PyObject *self) | |
470 | { | |
471 | xfree (((membuf_object *) self)->buffer); | |
472 | self->ob_type->tp_free (self); | |
473 | } | |
474 | ||
475 | /* Return a description of the Membuf object. */ | |
476 | static PyObject * | |
477 | mbpy_str (PyObject *self) | |
478 | { | |
479 | membuf_object *membuf_obj = (membuf_object *) self; | |
480 | ||
481 | return PyString_FromFormat (_("Memory buffer for address %s, \ | |
482 | which is %s bytes long."), | |
483 | paddress (python_gdbarch, membuf_obj->addr), | |
484 | pulongest (membuf_obj->length)); | |
485 | } | |
486 | ||
487 | static Py_ssize_t | |
488 | get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr) | |
489 | { | |
490 | membuf_object *membuf_obj = (membuf_object *) self; | |
491 | ||
492 | if (segment) | |
493 | { | |
494 | PyErr_SetString (PyExc_SystemError, | |
495 | _("The memory buffer supports only one segment.")); | |
496 | return -1; | |
497 | } | |
498 | ||
499 | *ptrptr = membuf_obj->buffer; | |
500 | ||
501 | return membuf_obj->length; | |
502 | } | |
503 | ||
504 | static Py_ssize_t | |
505 | get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr) | |
506 | { | |
507 | return get_read_buffer (self, segment, ptrptr); | |
508 | } | |
509 | ||
510 | static Py_ssize_t | |
511 | get_seg_count (PyObject *self, Py_ssize_t *lenp) | |
512 | { | |
513 | if (lenp) | |
514 | *lenp = ((membuf_object *) self)->length; | |
515 | ||
516 | return 1; | |
517 | } | |
518 | ||
519 | static Py_ssize_t | |
520 | get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr) | |
521 | { | |
522 | void *ptr = NULL; | |
523 | Py_ssize_t ret; | |
524 | ||
525 | ret = get_read_buffer (self, segment, &ptr); | |
526 | *ptrptr = (char *) ptr; | |
527 | ||
528 | return ret; | |
529 | } | |
530 | ||
531 | /* Implementation of | |
532 | gdb.search_memory (address, length, pattern). ADDRESS is the | |
533 | address to start the search. LENGTH specifies the scope of the | |
534 | search from ADDRESS. PATTERN is the pattern to search for (and | |
535 | must be a Python object supporting the buffer protocol). | |
536 | Returns a Python Long object holding the address where the pattern | |
8dc78533 JK |
537 | was located, or if the pattern was not found, returns None. Returns NULL |
538 | on error, with a python exception set. */ | |
595939de PM |
539 | static PyObject * |
540 | infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) | |
541 | { | |
542 | CORE_ADDR start_addr, length; | |
543 | static char *keywords[] = { "address", "length", "pattern", NULL }; | |
544 | PyObject *pattern, *start_addr_obj, *length_obj; | |
545 | volatile struct gdb_exception except; | |
546 | Py_ssize_t pattern_size; | |
547 | const void *buffer; | |
548 | CORE_ADDR found_addr; | |
549 | int found = 0; | |
550 | ||
551 | if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords, | |
552 | &start_addr_obj, &length_obj, | |
553 | &pattern)) | |
554 | return NULL; | |
555 | ||
556 | if (get_addr_from_python (start_addr_obj, &start_addr) | |
557 | && get_addr_from_python (length_obj, &length)) | |
558 | { | |
559 | if (!length) | |
560 | { | |
561 | PyErr_SetString (PyExc_ValueError, | |
562 | _("Search range is empty.")); | |
563 | return NULL; | |
564 | } | |
565 | /* Watch for overflows. */ | |
566 | else if (length > CORE_ADDR_MAX | |
567 | || (start_addr + length - 1) < start_addr) | |
568 | { | |
569 | PyErr_SetString (PyExc_ValueError, | |
570 | _("The search range is too large.")); | |
571 | ||
572 | return NULL; | |
573 | } | |
574 | } | |
575 | else | |
8dc78533 | 576 | return NULL; |
595939de PM |
577 | |
578 | if (!PyObject_CheckReadBuffer (pattern)) | |
579 | { | |
580 | PyErr_SetString (PyExc_RuntimeError, | |
581 | _("The pattern is not a Python buffer.")); | |
582 | ||
583 | return NULL; | |
584 | } | |
585 | ||
586 | if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1) | |
587 | return NULL; | |
588 | ||
589 | TRY_CATCH (except, RETURN_MASK_ALL) | |
590 | { | |
591 | found = target_search_memory (start_addr, length, | |
592 | buffer, pattern_size, | |
593 | &found_addr); | |
594 | } | |
595 | GDB_PY_HANDLE_EXCEPTION (except); | |
596 | ||
597 | if (found) | |
598 | return PyLong_FromLong (found_addr); | |
599 | else | |
600 | Py_RETURN_NONE; | |
601 | } | |
602 | ||
603 | ||
604 | /* Clear the INFERIOR pointer in an Inferior object and clear the | |
605 | thread list. */ | |
606 | static void | |
607 | py_free_inferior (struct inferior *inf, void *datum) | |
608 | { | |
609 | ||
610 | struct cleanup *cleanup; | |
611 | inferior_object *inf_obj = datum; | |
612 | struct threadlist_entry *th_entry, *th_tmp; | |
613 | ||
614 | cleanup = ensure_python_env (python_gdbarch, python_language); | |
615 | ||
616 | inf_obj->inferior = NULL; | |
617 | ||
618 | /* Deallocate threads list. */ | |
619 | for (th_entry = inf_obj->threads; th_entry != NULL;) | |
620 | { | |
621 | Py_DECREF (th_entry->thread_obj); | |
622 | ||
623 | th_tmp = th_entry; | |
624 | th_entry = th_entry->next; | |
625 | xfree (th_tmp); | |
626 | } | |
627 | ||
628 | inf_obj->nthreads = 0; | |
629 | ||
630 | Py_DECREF ((PyObject *) inf_obj); | |
631 | do_cleanups (cleanup); | |
632 | } | |
633 | ||
634 | void | |
635 | gdbpy_initialize_inferior (void) | |
636 | { | |
637 | if (PyType_Ready (&inferior_object_type) < 0) | |
638 | return; | |
639 | ||
640 | Py_INCREF (&inferior_object_type); | |
641 | PyModule_AddObject (gdb_module, "Inferior", | |
642 | (PyObject *) &inferior_object_type); | |
643 | ||
644 | infpy_inf_data_key = | |
645 | register_inferior_data_with_cleanup (py_free_inferior); | |
646 | ||
647 | observer_attach_new_thread (add_thread_object); | |
648 | observer_attach_thread_exit (delete_thread_object); | |
505500db SW |
649 | observer_attach_normal_stop (python_on_normal_stop); |
650 | observer_attach_target_resumed (python_on_resume); | |
651 | observer_attach_inferior_exit (python_inferior_exit); | |
595939de PM |
652 | |
653 | if (PyType_Ready (&membuf_object_type) < 0) | |
654 | return; | |
655 | ||
656 | Py_INCREF (&membuf_object_type); | |
657 | PyModule_AddObject (gdb_module, "Membuf", (PyObject *) | |
658 | &membuf_object_type); | |
659 | } | |
660 | ||
661 | static PyGetSetDef inferior_object_getset[] = | |
662 | { | |
663 | { "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL }, | |
664 | { "pid", infpy_get_pid, NULL, "PID of inferior, as assigned by the OS.", | |
665 | NULL }, | |
666 | { "was_attached", infpy_get_was_attached, NULL, | |
667 | "True if the inferior was created using 'attach'.", NULL }, | |
668 | { NULL } | |
669 | }; | |
670 | ||
671 | static PyMethodDef inferior_object_methods[] = | |
672 | { | |
673 | { "threads", infpy_threads, METH_NOARGS, | |
674 | "Return all the threads of this inferior." }, | |
675 | { "read_memory", (PyCFunction) infpy_read_memory, | |
676 | METH_VARARGS | METH_KEYWORDS, | |
677 | "read_memory (address, length) -> buffer\n\ | |
678 | Return a buffer object for reading from the inferior's memory." }, | |
679 | { "write_memory", (PyCFunction) infpy_write_memory, | |
680 | METH_VARARGS | METH_KEYWORDS, | |
681 | "write_memory (address, buffer [, length])\n\ | |
682 | Write the given buffer object to the inferior's memory." }, | |
683 | { "search_memory", (PyCFunction) infpy_search_memory, | |
684 | METH_VARARGS | METH_KEYWORDS, | |
685 | "search_memory (address, length, pattern) -> long\n\ | |
686 | Return a long with the address of a match, or None." }, | |
687 | { NULL } | |
688 | }; | |
689 | ||
690 | static PyTypeObject inferior_object_type = | |
691 | { | |
692 | PyObject_HEAD_INIT (NULL) | |
693 | 0, /* ob_size */ | |
694 | "gdb.Inferior", /* tp_name */ | |
695 | sizeof (inferior_object), /* tp_basicsize */ | |
696 | 0, /* tp_itemsize */ | |
697 | 0, /* tp_dealloc */ | |
698 | 0, /* tp_print */ | |
699 | 0, /* tp_getattr */ | |
700 | 0, /* tp_setattr */ | |
701 | 0, /* tp_compare */ | |
702 | 0, /* tp_repr */ | |
703 | 0, /* tp_as_number */ | |
704 | 0, /* tp_as_sequence */ | |
705 | 0, /* tp_as_mapping */ | |
706 | 0, /* tp_hash */ | |
707 | 0, /* tp_call */ | |
708 | 0, /* tp_str */ | |
709 | 0, /* tp_getattro */ | |
710 | 0, /* tp_setattro */ | |
711 | 0, /* tp_as_buffer */ | |
712 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER, /* tp_flags */ | |
713 | "GDB inferior object", /* tp_doc */ | |
714 | 0, /* tp_traverse */ | |
715 | 0, /* tp_clear */ | |
716 | 0, /* tp_richcompare */ | |
717 | 0, /* tp_weaklistoffset */ | |
718 | 0, /* tp_iter */ | |
719 | 0, /* tp_iternext */ | |
720 | inferior_object_methods, /* tp_methods */ | |
721 | 0, /* tp_members */ | |
722 | inferior_object_getset, /* tp_getset */ | |
723 | 0, /* tp_base */ | |
724 | 0, /* tp_dict */ | |
725 | 0, /* tp_descr_get */ | |
726 | 0, /* tp_descr_set */ | |
727 | 0, /* tp_dictoffset */ | |
728 | 0, /* tp_init */ | |
729 | 0 /* tp_alloc */ | |
730 | }; | |
731 | ||
732 | /* Python doesn't provide a decent way to get compatibility here. */ | |
733 | #if HAVE_LIBPYTHON2_4 | |
734 | #define CHARBUFFERPROC_NAME getcharbufferproc | |
735 | #else | |
736 | #define CHARBUFFERPROC_NAME charbufferproc | |
737 | #endif | |
738 | ||
739 | static PyBufferProcs buffer_procs = { | |
740 | get_read_buffer, | |
741 | get_write_buffer, | |
742 | get_seg_count, | |
743 | /* The cast here works around a difference between Python 2.4 and | |
744 | Python 2.5. */ | |
745 | (CHARBUFFERPROC_NAME) get_char_buffer | |
746 | }; | |
747 | ||
748 | static PyTypeObject membuf_object_type = { | |
749 | PyObject_HEAD_INIT (NULL) | |
750 | 0, /*ob_size*/ | |
751 | "gdb.Membuf", /*tp_name*/ | |
752 | sizeof (membuf_object), /*tp_basicsize*/ | |
753 | 0, /*tp_itemsize*/ | |
754 | mbpy_dealloc, /*tp_dealloc*/ | |
755 | 0, /*tp_print*/ | |
756 | 0, /*tp_getattr*/ | |
757 | 0, /*tp_setattr*/ | |
758 | 0, /*tp_compare*/ | |
759 | 0, /*tp_repr*/ | |
760 | 0, /*tp_as_number*/ | |
761 | 0, /*tp_as_sequence*/ | |
762 | 0, /*tp_as_mapping*/ | |
763 | 0, /*tp_hash */ | |
764 | 0, /*tp_call*/ | |
765 | mbpy_str, /*tp_str*/ | |
766 | 0, /*tp_getattro*/ | |
767 | 0, /*tp_setattro*/ | |
768 | &buffer_procs, /*tp_as_buffer*/ | |
769 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
770 | "GDB memory buffer object", /*tp_doc*/ | |
771 | 0, /* tp_traverse */ | |
772 | 0, /* tp_clear */ | |
773 | 0, /* tp_richcompare */ | |
774 | 0, /* tp_weaklistoffset */ | |
775 | 0, /* tp_iter */ | |
776 | 0, /* tp_iternext */ | |
777 | 0, /* tp_methods */ | |
778 | 0, /* tp_members */ | |
779 | 0, /* tp_getset */ | |
780 | 0, /* tp_base */ | |
781 | 0, /* tp_dict */ | |
782 | 0, /* tp_descr_get */ | |
783 | 0, /* tp_descr_set */ | |
784 | 0, /* tp_dictoffset */ | |
785 | 0, /* tp_init */ | |
786 | 0, /* tp_alloc */ | |
787 | PyType_GenericNew /* tp_new */ | |
788 | }; |