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