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