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