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