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