py-breakpoint.c revision 1.1.1.8 1 1.1 christos /* Python interface to breakpoints
2 1.1 christos
3 1.1.1.8 christos Copyright (C) 2008-2023 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 "value.h"
22 1.1 christos #include "python-internal.h"
23 1.1 christos #include "python.h"
24 1.1 christos #include "charset.h"
25 1.1 christos #include "breakpoint.h"
26 1.1 christos #include "gdbcmd.h"
27 1.1 christos #include "gdbthread.h"
28 1.1.1.6 christos #include "observable.h"
29 1.1 christos #include "cli/cli-script.h"
30 1.1 christos #include "ada-lang.h"
31 1.1 christos #include "arch-utils.h"
32 1.1 christos #include "language.h"
33 1.1.1.4 christos #include "location.h"
34 1.1.1.4 christos #include "py-event.h"
35 1.1.1.6 christos #include "linespec.h"
36 1.1 christos
37 1.1.1.8 christos extern PyTypeObject breakpoint_location_object_type
38 1.1.1.8 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("breakpoint_location_object");
39 1.1.1.8 christos
40 1.1.1.8 christos struct gdbpy_breakpoint_location_object
41 1.1.1.8 christos {
42 1.1.1.8 christos PyObject_HEAD
43 1.1.1.8 christos
44 1.1.1.8 christos /* An owning reference to the gdb breakpoint location object. */
45 1.1.1.8 christos bp_location *bp_loc;
46 1.1.1.8 christos
47 1.1.1.8 christos /* An owning reference to the location's breakpoint owner. */
48 1.1.1.8 christos gdbpy_breakpoint_object *owner;
49 1.1.1.8 christos };
50 1.1.1.8 christos
51 1.1.1.8 christos /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python
52 1.1.1.8 christos exception if they are not. */
53 1.1.1.8 christos #define BPLOCPY_REQUIRE_VALID(Breakpoint, Location) \
54 1.1.1.8 christos do { \
55 1.1.1.8 christos if ((Breakpoint)->bp != (Location)->bp_loc->owner) \
56 1.1.1.8 christos return PyErr_Format (PyExc_RuntimeError, \
57 1.1.1.8 christos _("Breakpoint location is invalid.")); \
58 1.1.1.8 christos } while (0)
59 1.1.1.8 christos
60 1.1.1.8 christos /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python
61 1.1.1.8 christos exception if they are not. This macro is for use in setter functions. */
62 1.1.1.8 christos #define BPLOCPY_SET_REQUIRE_VALID(Breakpoint, Location) \
63 1.1.1.8 christos do { \
64 1.1.1.8 christos if ((Breakpoint)->bp != (Location)->bp_loc->owner) \
65 1.1.1.8 christos { \
66 1.1.1.8 christos PyErr_Format (PyExc_RuntimeError, \
67 1.1.1.8 christos _("Breakpoint location is invalid.")); \
68 1.1.1.8 christos return -1; \
69 1.1.1.8 christos } \
70 1.1.1.8 christos } while (0)
71 1.1.1.8 christos
72 1.1.1.8 christos /* Debugging of Python breakpoints. */
73 1.1.1.8 christos
74 1.1.1.8 christos static bool pybp_debug;
75 1.1.1.8 christos
76 1.1.1.8 christos /* Implementation of "show debug py-breakpoint". */
77 1.1.1.8 christos
78 1.1.1.8 christos static void
79 1.1.1.8 christos show_pybp_debug (struct ui_file *file, int from_tty,
80 1.1.1.8 christos struct cmd_list_element *c, const char *value)
81 1.1.1.8 christos {
82 1.1.1.8 christos gdb_printf (file, _("Python breakpoint debugging is %s.\n"), value);
83 1.1.1.8 christos }
84 1.1.1.8 christos
85 1.1.1.8 christos /* Print a "py-breakpoint" debug statement. */
86 1.1.1.8 christos
87 1.1.1.8 christos #define pybp_debug_printf(fmt, ...) \
88 1.1.1.8 christos debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__)
89 1.1.1.8 christos
90 1.1.1.8 christos /* Print a "py-breakpoint" enter/exit debug statements. */
91 1.1.1.8 christos
92 1.1.1.8 christos #define PYBP_SCOPED_DEBUG_ENTER_EXIT \
93 1.1.1.8 christos scoped_debug_enter_exit (pybp_debug, "py-breakpoint")
94 1.1.1.8 christos
95 1.1 christos /* Number of live breakpoints. */
96 1.1 christos static int bppy_live;
97 1.1 christos
98 1.1 christos /* Variables used to pass information between the Breakpoint
99 1.1 christos constructor and the breakpoint-created hook function. */
100 1.1 christos gdbpy_breakpoint_object *bppy_pending_object;
101 1.1 christos
102 1.1 christos /* Function that is called when a Python condition is evaluated. */
103 1.1.1.5 christos static const char stop_func[] = "stop";
104 1.1 christos
105 1.1 christos /* This is used to initialize various gdb.bp_* constants. */
106 1.1 christos struct pybp_code
107 1.1 christos {
108 1.1 christos /* The name. */
109 1.1 christos const char *name;
110 1.1 christos /* The code. */
111 1.1 christos int code;
112 1.1 christos };
113 1.1 christos
114 1.1 christos /* Entries related to the type of user set breakpoints. */
115 1.1 christos static struct pybp_code pybp_codes[] =
116 1.1 christos {
117 1.1 christos { "BP_NONE", bp_none},
118 1.1 christos { "BP_BREAKPOINT", bp_breakpoint},
119 1.1.1.8 christos { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint},
120 1.1 christos { "BP_WATCHPOINT", bp_watchpoint},
121 1.1 christos { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
122 1.1 christos { "BP_READ_WATCHPOINT", bp_read_watchpoint},
123 1.1 christos { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
124 1.1.1.8 christos { "BP_CATCHPOINT", bp_catchpoint},
125 1.1 christos {NULL} /* Sentinel. */
126 1.1 christos };
127 1.1 christos
128 1.1 christos /* Entries related to the type of watchpoint. */
129 1.1 christos static struct pybp_code pybp_watch_types[] =
130 1.1 christos {
131 1.1 christos { "WP_READ", hw_read},
132 1.1 christos { "WP_WRITE", hw_write},
133 1.1 christos { "WP_ACCESS", hw_access},
134 1.1 christos {NULL} /* Sentinel. */
135 1.1 christos };
136 1.1 christos
137 1.1 christos /* Python function which checks the validity of a breakpoint object. */
138 1.1 christos static PyObject *
139 1.1 christos bppy_is_valid (PyObject *self, PyObject *args)
140 1.1 christos {
141 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
142 1.1 christos
143 1.1 christos if (self_bp->bp)
144 1.1 christos Py_RETURN_TRUE;
145 1.1 christos Py_RETURN_FALSE;
146 1.1 christos }
147 1.1 christos
148 1.1 christos /* Python function to test whether or not the breakpoint is enabled. */
149 1.1 christos static PyObject *
150 1.1 christos bppy_get_enabled (PyObject *self, void *closure)
151 1.1 christos {
152 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
153 1.1 christos
154 1.1 christos BPPY_REQUIRE_VALID (self_bp);
155 1.1 christos if (! self_bp->bp)
156 1.1 christos Py_RETURN_FALSE;
157 1.1 christos if (self_bp->bp->enable_state == bp_enabled)
158 1.1 christos Py_RETURN_TRUE;
159 1.1 christos Py_RETURN_FALSE;
160 1.1 christos }
161 1.1 christos
162 1.1 christos /* Python function to test whether or not the breakpoint is silent. */
163 1.1 christos static PyObject *
164 1.1 christos bppy_get_silent (PyObject *self, void *closure)
165 1.1 christos {
166 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
167 1.1 christos
168 1.1 christos BPPY_REQUIRE_VALID (self_bp);
169 1.1 christos if (self_bp->bp->silent)
170 1.1 christos Py_RETURN_TRUE;
171 1.1 christos Py_RETURN_FALSE;
172 1.1 christos }
173 1.1 christos
174 1.1 christos /* Python function to set the enabled state of a breakpoint. */
175 1.1 christos static int
176 1.1 christos bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
177 1.1 christos {
178 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
179 1.1 christos int cmp;
180 1.1 christos
181 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
182 1.1 christos
183 1.1 christos if (newvalue == NULL)
184 1.1 christos {
185 1.1 christos PyErr_SetString (PyExc_TypeError,
186 1.1 christos _("Cannot delete `enabled' attribute."));
187 1.1 christos
188 1.1 christos return -1;
189 1.1 christos }
190 1.1 christos else if (! PyBool_Check (newvalue))
191 1.1 christos {
192 1.1 christos PyErr_SetString (PyExc_TypeError,
193 1.1 christos _("The value of `enabled' must be a boolean."));
194 1.1 christos return -1;
195 1.1 christos }
196 1.1 christos
197 1.1 christos cmp = PyObject_IsTrue (newvalue);
198 1.1 christos if (cmp < 0)
199 1.1 christos return -1;
200 1.1 christos
201 1.1.1.7 christos try
202 1.1 christos {
203 1.1 christos if (cmp == 1)
204 1.1 christos enable_breakpoint (self_bp->bp);
205 1.1 christos else
206 1.1 christos disable_breakpoint (self_bp->bp);
207 1.1 christos }
208 1.1.1.7 christos catch (const gdb_exception &except)
209 1.1.1.3 christos {
210 1.1.1.3 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
211 1.1.1.3 christos }
212 1.1 christos
213 1.1 christos return 0;
214 1.1 christos }
215 1.1 christos
216 1.1 christos /* Python function to set the 'silent' state of a breakpoint. */
217 1.1 christos static int
218 1.1 christos bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
219 1.1 christos {
220 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
221 1.1 christos int cmp;
222 1.1 christos
223 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
224 1.1 christos
225 1.1 christos if (newvalue == NULL)
226 1.1 christos {
227 1.1 christos PyErr_SetString (PyExc_TypeError,
228 1.1 christos _("Cannot delete `silent' attribute."));
229 1.1 christos return -1;
230 1.1 christos }
231 1.1 christos else if (! PyBool_Check (newvalue))
232 1.1 christos {
233 1.1 christos PyErr_SetString (PyExc_TypeError,
234 1.1 christos _("The value of `silent' must be a boolean."));
235 1.1 christos return -1;
236 1.1 christos }
237 1.1 christos
238 1.1 christos cmp = PyObject_IsTrue (newvalue);
239 1.1 christos if (cmp < 0)
240 1.1 christos return -1;
241 1.1 christos else
242 1.1 christos breakpoint_set_silent (self_bp->bp, cmp);
243 1.1 christos
244 1.1 christos return 0;
245 1.1 christos }
246 1.1 christos
247 1.1 christos /* Python function to set the thread of a breakpoint. */
248 1.1 christos static int
249 1.1 christos bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
250 1.1 christos {
251 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
252 1.1 christos long id;
253 1.1 christos
254 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
255 1.1 christos
256 1.1 christos if (newvalue == NULL)
257 1.1 christos {
258 1.1 christos PyErr_SetString (PyExc_TypeError,
259 1.1 christos _("Cannot delete `thread' attribute."));
260 1.1 christos return -1;
261 1.1 christos }
262 1.1.1.8 christos else if (PyLong_Check (newvalue))
263 1.1 christos {
264 1.1 christos if (! gdb_py_int_as_long (newvalue, &id))
265 1.1 christos return -1;
266 1.1 christos
267 1.1.1.4 christos if (!valid_global_thread_id (id))
268 1.1 christos {
269 1.1 christos PyErr_SetString (PyExc_RuntimeError,
270 1.1 christos _("Invalid thread ID."));
271 1.1 christos return -1;
272 1.1 christos }
273 1.1 christos }
274 1.1 christos else if (newvalue == Py_None)
275 1.1 christos id = -1;
276 1.1 christos else
277 1.1 christos {
278 1.1 christos PyErr_SetString (PyExc_TypeError,
279 1.1 christos _("The value of `thread' must be an integer or None."));
280 1.1 christos return -1;
281 1.1 christos }
282 1.1 christos
283 1.1 christos breakpoint_set_thread (self_bp->bp, id);
284 1.1 christos
285 1.1 christos return 0;
286 1.1 christos }
287 1.1 christos
288 1.1 christos /* Python function to set the (Ada) task of a breakpoint. */
289 1.1 christos static int
290 1.1 christos bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
291 1.1 christos {
292 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
293 1.1 christos long id;
294 1.1 christos int valid_id = 0;
295 1.1 christos
296 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
297 1.1 christos
298 1.1 christos if (newvalue == NULL)
299 1.1 christos {
300 1.1 christos PyErr_SetString (PyExc_TypeError,
301 1.1 christos _("Cannot delete `task' attribute."));
302 1.1 christos return -1;
303 1.1 christos }
304 1.1.1.8 christos else if (PyLong_Check (newvalue))
305 1.1 christos {
306 1.1 christos if (! gdb_py_int_as_long (newvalue, &id))
307 1.1 christos return -1;
308 1.1 christos
309 1.1.1.7 christos try
310 1.1 christos {
311 1.1 christos valid_id = valid_task_id (id);
312 1.1 christos }
313 1.1.1.7 christos catch (const gdb_exception &except)
314 1.1.1.3 christos {
315 1.1.1.3 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
316 1.1.1.3 christos }
317 1.1 christos
318 1.1 christos if (! valid_id)
319 1.1 christos {
320 1.1 christos PyErr_SetString (PyExc_RuntimeError,
321 1.1 christos _("Invalid task ID."));
322 1.1 christos return -1;
323 1.1 christos }
324 1.1 christos }
325 1.1 christos else if (newvalue == Py_None)
326 1.1 christos id = 0;
327 1.1 christos else
328 1.1 christos {
329 1.1 christos PyErr_SetString (PyExc_TypeError,
330 1.1 christos _("The value of `task' must be an integer or None."));
331 1.1 christos return -1;
332 1.1 christos }
333 1.1 christos
334 1.1 christos breakpoint_set_task (self_bp->bp, id);
335 1.1 christos
336 1.1 christos return 0;
337 1.1 christos }
338 1.1 christos
339 1.1 christos /* Python function which deletes the underlying GDB breakpoint. This
340 1.1 christos triggers the breakpoint_deleted observer which will call
341 1.1 christos gdbpy_breakpoint_deleted; that function cleans up the Python
342 1.1 christos sections. */
343 1.1 christos
344 1.1 christos static PyObject *
345 1.1 christos bppy_delete_breakpoint (PyObject *self, PyObject *args)
346 1.1 christos {
347 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
348 1.1 christos
349 1.1 christos BPPY_REQUIRE_VALID (self_bp);
350 1.1 christos
351 1.1.1.7 christos try
352 1.1 christos {
353 1.1 christos delete_breakpoint (self_bp->bp);
354 1.1 christos }
355 1.1.1.7 christos catch (const gdb_exception &except)
356 1.1.1.3 christos {
357 1.1.1.3 christos GDB_PY_HANDLE_EXCEPTION (except);
358 1.1.1.3 christos }
359 1.1 christos
360 1.1 christos Py_RETURN_NONE;
361 1.1 christos }
362 1.1 christos
363 1.1 christos
364 1.1 christos /* Python function to set the ignore count of a breakpoint. */
365 1.1 christos static int
366 1.1 christos bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
367 1.1 christos {
368 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
369 1.1 christos long value;
370 1.1 christos
371 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
372 1.1 christos
373 1.1 christos if (newvalue == NULL)
374 1.1 christos {
375 1.1 christos PyErr_SetString (PyExc_TypeError,
376 1.1 christos _("Cannot delete `ignore_count' attribute."));
377 1.1 christos return -1;
378 1.1 christos }
379 1.1.1.8 christos else if (!PyLong_Check (newvalue))
380 1.1 christos {
381 1.1 christos PyErr_SetString (PyExc_TypeError,
382 1.1 christos _("The value of `ignore_count' must be an integer."));
383 1.1 christos return -1;
384 1.1 christos }
385 1.1 christos
386 1.1 christos if (! gdb_py_int_as_long (newvalue, &value))
387 1.1 christos return -1;
388 1.1 christos
389 1.1 christos if (value < 0)
390 1.1 christos value = 0;
391 1.1 christos
392 1.1.1.7 christos try
393 1.1 christos {
394 1.1 christos set_ignore_count (self_bp->number, (int) value, 0);
395 1.1 christos }
396 1.1.1.7 christos catch (const gdb_exception &except)
397 1.1.1.3 christos {
398 1.1.1.3 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
399 1.1.1.3 christos }
400 1.1 christos
401 1.1 christos return 0;
402 1.1 christos }
403 1.1 christos
404 1.1 christos /* Python function to set the hit count of a breakpoint. */
405 1.1 christos static int
406 1.1 christos bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
407 1.1 christos {
408 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
409 1.1 christos
410 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
411 1.1 christos
412 1.1 christos if (newvalue == NULL)
413 1.1 christos {
414 1.1 christos PyErr_SetString (PyExc_TypeError,
415 1.1 christos _("Cannot delete `hit_count' attribute."));
416 1.1 christos return -1;
417 1.1 christos }
418 1.1 christos else
419 1.1 christos {
420 1.1 christos long value;
421 1.1 christos
422 1.1 christos if (! gdb_py_int_as_long (newvalue, &value))
423 1.1 christos return -1;
424 1.1 christos
425 1.1 christos if (value != 0)
426 1.1 christos {
427 1.1 christos PyErr_SetString (PyExc_AttributeError,
428 1.1 christos _("The value of `hit_count' must be zero."));
429 1.1 christos return -1;
430 1.1 christos }
431 1.1 christos }
432 1.1 christos
433 1.1 christos self_bp->bp->hit_count = 0;
434 1.1 christos
435 1.1 christos return 0;
436 1.1 christos }
437 1.1 christos
438 1.1 christos /* Python function to get the location of a breakpoint. */
439 1.1 christos static PyObject *
440 1.1 christos bppy_get_location (PyObject *self, void *closure)
441 1.1 christos {
442 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
443 1.1 christos
444 1.1 christos BPPY_REQUIRE_VALID (obj);
445 1.1 christos
446 1.1.1.8 christos if (obj->bp->type != bp_breakpoint
447 1.1.1.8 christos && obj->bp->type != bp_hardware_breakpoint)
448 1.1 christos Py_RETURN_NONE;
449 1.1 christos
450 1.1.1.8 christos const char *str = obj->bp->locspec->to_string ();
451 1.1.1.8 christos if (str == nullptr)
452 1.1 christos str = "";
453 1.1.1.6 christos return host_string_to_python_string (str).release ();
454 1.1 christos }
455 1.1 christos
456 1.1 christos /* Python function to get the breakpoint expression. */
457 1.1 christos static PyObject *
458 1.1 christos bppy_get_expression (PyObject *self, void *closure)
459 1.1 christos {
460 1.1.1.5 christos const char *str;
461 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
462 1.1 christos struct watchpoint *wp;
463 1.1 christos
464 1.1 christos BPPY_REQUIRE_VALID (obj);
465 1.1 christos
466 1.1 christos if (!is_watchpoint (obj->bp))
467 1.1 christos Py_RETURN_NONE;
468 1.1 christos
469 1.1 christos wp = (struct watchpoint *) obj->bp;
470 1.1 christos
471 1.1.1.8 christos str = wp->exp_string.get ();
472 1.1 christos if (! str)
473 1.1 christos str = "";
474 1.1 christos
475 1.1.1.6 christos return host_string_to_python_string (str).release ();
476 1.1 christos }
477 1.1 christos
478 1.1 christos /* Python function to get the condition expression of a breakpoint. */
479 1.1 christos static PyObject *
480 1.1 christos bppy_get_condition (PyObject *self, void *closure)
481 1.1 christos {
482 1.1 christos char *str;
483 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
484 1.1 christos
485 1.1 christos BPPY_REQUIRE_VALID (obj);
486 1.1 christos
487 1.1.1.8 christos str = obj->bp->cond_string.get ();
488 1.1 christos if (! str)
489 1.1 christos Py_RETURN_NONE;
490 1.1 christos
491 1.1.1.6 christos return host_string_to_python_string (str).release ();
492 1.1 christos }
493 1.1 christos
494 1.1 christos /* Returns 0 on success. Returns -1 on error, with a python exception set.
495 1.1 christos */
496 1.1 christos
497 1.1 christos static int
498 1.1 christos bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
499 1.1 christos {
500 1.1.1.5 christos gdb::unique_xmalloc_ptr<char> exp_holder;
501 1.1.1.5 christos const char *exp = NULL;
502 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
503 1.1.1.7 christos struct gdb_exception except;
504 1.1 christos
505 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp);
506 1.1 christos
507 1.1 christos if (newvalue == NULL)
508 1.1 christos {
509 1.1 christos PyErr_SetString (PyExc_TypeError,
510 1.1 christos _("Cannot delete `condition' attribute."));
511 1.1 christos return -1;
512 1.1 christos }
513 1.1 christos else if (newvalue == Py_None)
514 1.1 christos exp = "";
515 1.1 christos else
516 1.1 christos {
517 1.1.1.5 christos exp_holder = python_string_to_host_string (newvalue);
518 1.1.1.5 christos if (exp_holder == NULL)
519 1.1 christos return -1;
520 1.1.1.5 christos exp = exp_holder.get ();
521 1.1 christos }
522 1.1 christos
523 1.1.1.7 christos try
524 1.1 christos {
525 1.1.1.8 christos set_breakpoint_condition (self_bp->bp, exp, 0, false);
526 1.1 christos }
527 1.1.1.7 christos catch (gdb_exception &ex)
528 1.1.1.3 christos {
529 1.1.1.7 christos except = std::move (ex);
530 1.1.1.3 christos }
531 1.1 christos
532 1.1 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
533 1.1 christos
534 1.1 christos return 0;
535 1.1 christos }
536 1.1 christos
537 1.1 christos /* Python function to get the commands attached to a breakpoint. */
538 1.1 christos static PyObject *
539 1.1 christos bppy_get_commands (PyObject *self, void *closure)
540 1.1 christos {
541 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
542 1.1 christos struct breakpoint *bp = self_bp->bp;
543 1.1 christos
544 1.1 christos BPPY_REQUIRE_VALID (self_bp);
545 1.1 christos
546 1.1 christos if (! self_bp->bp->commands)
547 1.1 christos Py_RETURN_NONE;
548 1.1 christos
549 1.1.1.5 christos string_file stb;
550 1.1 christos
551 1.1.1.7 christos try
552 1.1 christos {
553 1.1.1.8 christos ui_out_redirect_pop redir (current_uiout, &stb);
554 1.1 christos print_command_lines (current_uiout, breakpoint_commands (bp), 0);
555 1.1 christos }
556 1.1.1.7 christos catch (const gdb_exception &except)
557 1.1 christos {
558 1.1 christos gdbpy_convert_exception (except);
559 1.1 christos return NULL;
560 1.1 christos }
561 1.1 christos
562 1.1.1.6 christos return host_string_to_python_string (stb.c_str ()).release ();
563 1.1.1.6 christos }
564 1.1.1.6 christos
565 1.1.1.6 christos /* Set the commands attached to a breakpoint. Returns 0 on success.
566 1.1.1.6 christos Returns -1 on error, with a python exception set. */
567 1.1.1.6 christos static int
568 1.1.1.6 christos bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
569 1.1.1.6 christos {
570 1.1.1.6 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
571 1.1.1.7 christos struct gdb_exception except;
572 1.1.1.6 christos
573 1.1.1.6 christos BPPY_SET_REQUIRE_VALID (self_bp);
574 1.1.1.6 christos
575 1.1.1.6 christos gdb::unique_xmalloc_ptr<char> commands
576 1.1.1.6 christos (python_string_to_host_string (newvalue));
577 1.1.1.6 christos if (commands == nullptr)
578 1.1.1.6 christos return -1;
579 1.1.1.6 christos
580 1.1.1.7 christos try
581 1.1.1.6 christos {
582 1.1.1.6 christos bool first = true;
583 1.1.1.6 christos char *save_ptr = nullptr;
584 1.1.1.6 christos auto reader
585 1.1.1.8 christos = [&] (std::string &buffer)
586 1.1.1.6 christos {
587 1.1.1.6 christos const char *result = strtok_r (first ? commands.get () : nullptr,
588 1.1.1.6 christos "\n", &save_ptr);
589 1.1.1.6 christos first = false;
590 1.1.1.6 christos return result;
591 1.1.1.6 christos };
592 1.1.1.6 christos
593 1.1.1.6 christos counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
594 1.1.1.6 christos breakpoint_set_commands (self_bp->bp, std::move (lines));
595 1.1.1.6 christos }
596 1.1.1.7 christos catch (gdb_exception &ex)
597 1.1.1.6 christos {
598 1.1.1.7 christos except = std::move (ex);
599 1.1.1.6 christos }
600 1.1.1.6 christos
601 1.1.1.6 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
602 1.1.1.6 christos
603 1.1.1.6 christos return 0;
604 1.1 christos }
605 1.1 christos
606 1.1 christos /* Python function to get the breakpoint type. */
607 1.1 christos static PyObject *
608 1.1 christos bppy_get_type (PyObject *self, void *closure)
609 1.1 christos {
610 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
611 1.1 christos
612 1.1 christos BPPY_REQUIRE_VALID (self_bp);
613 1.1 christos
614 1.1.1.8 christos return gdb_py_object_from_longest (self_bp->bp->type).release ();
615 1.1 christos }
616 1.1 christos
617 1.1 christos /* Python function to get the visibility of the breakpoint. */
618 1.1 christos
619 1.1 christos static PyObject *
620 1.1 christos bppy_get_visibility (PyObject *self, void *closure)
621 1.1 christos {
622 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
623 1.1 christos
624 1.1 christos BPPY_REQUIRE_VALID (self_bp);
625 1.1 christos
626 1.1.1.4 christos if (user_breakpoint_p (self_bp->bp))
627 1.1.1.4 christos Py_RETURN_TRUE;
628 1.1 christos
629 1.1.1.4 christos Py_RETURN_FALSE;
630 1.1 christos }
631 1.1 christos
632 1.1 christos /* Python function to determine if the breakpoint is a temporary
633 1.1 christos breakpoint. */
634 1.1 christos
635 1.1 christos static PyObject *
636 1.1 christos bppy_get_temporary (PyObject *self, void *closure)
637 1.1 christos {
638 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
639 1.1 christos
640 1.1 christos BPPY_REQUIRE_VALID (self_bp);
641 1.1 christos
642 1.1 christos if (self_bp->bp->disposition == disp_del
643 1.1 christos || self_bp->bp->disposition == disp_del_at_next_stop)
644 1.1 christos Py_RETURN_TRUE;
645 1.1 christos
646 1.1 christos Py_RETURN_FALSE;
647 1.1 christos }
648 1.1 christos
649 1.1.1.4 christos /* Python function to determine if the breakpoint is a pending
650 1.1.1.4 christos breakpoint. */
651 1.1.1.4 christos
652 1.1.1.4 christos static PyObject *
653 1.1.1.4 christos bppy_get_pending (PyObject *self, void *closure)
654 1.1.1.4 christos {
655 1.1.1.4 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
656 1.1.1.4 christos
657 1.1.1.4 christos BPPY_REQUIRE_VALID (self_bp);
658 1.1.1.4 christos
659 1.1.1.4 christos if (is_watchpoint (self_bp->bp))
660 1.1.1.4 christos Py_RETURN_FALSE;
661 1.1.1.4 christos if (pending_breakpoint_p (self_bp->bp))
662 1.1.1.4 christos Py_RETURN_TRUE;
663 1.1.1.4 christos
664 1.1.1.4 christos Py_RETURN_FALSE;
665 1.1.1.4 christos }
666 1.1.1.4 christos
667 1.1 christos /* Python function to get the breakpoint's number. */
668 1.1 christos static PyObject *
669 1.1 christos bppy_get_number (PyObject *self, void *closure)
670 1.1 christos {
671 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
672 1.1 christos
673 1.1 christos BPPY_REQUIRE_VALID (self_bp);
674 1.1 christos
675 1.1.1.8 christos return gdb_py_object_from_longest (self_bp->number).release ();
676 1.1 christos }
677 1.1 christos
678 1.1 christos /* Python function to get the breakpoint's thread ID. */
679 1.1 christos static PyObject *
680 1.1 christos bppy_get_thread (PyObject *self, void *closure)
681 1.1 christos {
682 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
683 1.1 christos
684 1.1 christos BPPY_REQUIRE_VALID (self_bp);
685 1.1 christos
686 1.1 christos if (self_bp->bp->thread == -1)
687 1.1 christos Py_RETURN_NONE;
688 1.1 christos
689 1.1.1.8 christos return gdb_py_object_from_longest (self_bp->bp->thread).release ();
690 1.1 christos }
691 1.1 christos
692 1.1 christos /* Python function to get the breakpoint's task ID (in Ada). */
693 1.1 christos static PyObject *
694 1.1 christos bppy_get_task (PyObject *self, void *closure)
695 1.1 christos {
696 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
697 1.1 christos
698 1.1 christos BPPY_REQUIRE_VALID (self_bp);
699 1.1 christos
700 1.1 christos if (self_bp->bp->task == 0)
701 1.1 christos Py_RETURN_NONE;
702 1.1 christos
703 1.1.1.8 christos return gdb_py_object_from_longest (self_bp->bp->task).release ();
704 1.1 christos }
705 1.1 christos
706 1.1 christos /* Python function to get the breakpoint's hit count. */
707 1.1 christos static PyObject *
708 1.1 christos bppy_get_hit_count (PyObject *self, void *closure)
709 1.1 christos {
710 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
711 1.1 christos
712 1.1 christos BPPY_REQUIRE_VALID (self_bp);
713 1.1 christos
714 1.1.1.8 christos return gdb_py_object_from_longest (self_bp->bp->hit_count).release ();
715 1.1 christos }
716 1.1 christos
717 1.1 christos /* Python function to get the breakpoint's ignore count. */
718 1.1 christos static PyObject *
719 1.1 christos bppy_get_ignore_count (PyObject *self, void *closure)
720 1.1 christos {
721 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
722 1.1 christos
723 1.1 christos BPPY_REQUIRE_VALID (self_bp);
724 1.1 christos
725 1.1.1.8 christos return gdb_py_object_from_longest (self_bp->bp->ignore_count).release ();
726 1.1.1.8 christos }
727 1.1.1.8 christos
728 1.1.1.8 christos /* Python function to get the breakpoint locations of an owner breakpoint. */
729 1.1.1.8 christos
730 1.1.1.8 christos static PyObject *
731 1.1.1.8 christos bppy_get_locations (PyObject *self, void *closure)
732 1.1.1.8 christos {
733 1.1.1.8 christos using py_bploc_t = gdbpy_breakpoint_location_object;
734 1.1.1.8 christos auto *self_bp = (gdbpy_breakpoint_object *) self;
735 1.1.1.8 christos BPPY_REQUIRE_VALID (self_bp);
736 1.1.1.8 christos
737 1.1.1.8 christos gdbpy_ref<> list (PyList_New (0));
738 1.1.1.8 christos if (list == nullptr)
739 1.1.1.8 christos return nullptr;
740 1.1.1.8 christos
741 1.1.1.8 christos for (bp_location *loc : self_bp->bp->locations ())
742 1.1.1.8 christos {
743 1.1.1.8 christos gdbpy_ref<py_bploc_t> py_bploc
744 1.1.1.8 christos (PyObject_New (py_bploc_t, &breakpoint_location_object_type));
745 1.1.1.8 christos if (py_bploc == nullptr)
746 1.1.1.8 christos return nullptr;
747 1.1.1.8 christos
748 1.1.1.8 christos bp_location_ref_ptr ref = bp_location_ref_ptr::new_reference (loc);
749 1.1.1.8 christos /* The location takes a reference to the owner breakpoint.
750 1.1.1.8 christos Decrements when they are de-allocated in bplocpy_dealloc */
751 1.1.1.8 christos Py_INCREF (self);
752 1.1.1.8 christos py_bploc->owner = self_bp;
753 1.1.1.8 christos py_bploc->bp_loc = ref.release ();
754 1.1.1.8 christos if (PyList_Append (list.get (), (PyObject *) py_bploc.get ()) != 0)
755 1.1.1.8 christos return nullptr;
756 1.1.1.8 christos }
757 1.1.1.8 christos return list.release ();
758 1.1 christos }
759 1.1 christos
760 1.1.1.6 christos /* Internal function to validate the Python parameters/keywords
761 1.1.1.6 christos provided to bppy_init. */
762 1.1.1.6 christos
763 1.1.1.6 christos static int
764 1.1.1.6 christos bppy_init_validate_args (const char *spec, char *source,
765 1.1.1.6 christos char *function, char *label,
766 1.1.1.6 christos char *line, enum bptype type)
767 1.1.1.6 christos {
768 1.1.1.6 christos /* If spec is defined, ensure that none of the explicit location
769 1.1.1.6 christos keywords are also defined. */
770 1.1.1.6 christos if (spec != NULL)
771 1.1.1.6 christos {
772 1.1.1.6 christos if (source != NULL || function != NULL || label != NULL || line != NULL)
773 1.1.1.6 christos {
774 1.1.1.6 christos PyErr_SetString (PyExc_RuntimeError,
775 1.1.1.6 christos _("Breakpoints specified with spec cannot "
776 1.1.1.6 christos "have source, function, label or line defined."));
777 1.1.1.6 christos return -1;
778 1.1.1.6 christos }
779 1.1.1.6 christos }
780 1.1.1.6 christos else
781 1.1.1.6 christos {
782 1.1.1.6 christos /* If spec isn't defined, ensure that the user is not trying to
783 1.1.1.6 christos define a watchpoint with an explicit location. */
784 1.1.1.6 christos if (type == bp_watchpoint)
785 1.1.1.6 christos {
786 1.1.1.6 christos PyErr_SetString (PyExc_RuntimeError,
787 1.1.1.6 christos _("Watchpoints cannot be set by explicit "
788 1.1.1.6 christos "location parameters."));
789 1.1.1.6 christos return -1;
790 1.1.1.6 christos }
791 1.1.1.6 christos else
792 1.1.1.6 christos {
793 1.1.1.6 christos /* Otherwise, ensure some explicit locations are defined. */
794 1.1.1.6 christos if (source == NULL && function == NULL && label == NULL
795 1.1.1.6 christos && line == NULL)
796 1.1.1.6 christos {
797 1.1.1.6 christos PyErr_SetString (PyExc_RuntimeError,
798 1.1.1.6 christos _("Neither spec nor explicit location set."));
799 1.1.1.6 christos return -1;
800 1.1.1.6 christos }
801 1.1.1.6 christos /* Finally, if source is specified, ensure that line, label
802 1.1.1.6 christos or function are specified too. */
803 1.1.1.6 christos if (source != NULL && function == NULL && label == NULL
804 1.1.1.6 christos && line == NULL)
805 1.1.1.6 christos {
806 1.1.1.6 christos PyErr_SetString (PyExc_RuntimeError,
807 1.1.1.6 christos _("Specifying a source must also include a "
808 1.1.1.6 christos "line, label or function."));
809 1.1.1.6 christos return -1;
810 1.1.1.6 christos }
811 1.1.1.6 christos }
812 1.1.1.6 christos }
813 1.1.1.6 christos return 1;
814 1.1.1.6 christos }
815 1.1.1.6 christos
816 1.1 christos /* Python function to create a new breakpoint. */
817 1.1 christos static int
818 1.1 christos bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
819 1.1 christos {
820 1.1.1.5 christos static const char *keywords[] = { "spec", "type", "wp_class", "internal",
821 1.1.1.6 christos "temporary","source", "function",
822 1.1.1.6 christos "label", "line", "qualified", NULL };
823 1.1.1.6 christos const char *spec = NULL;
824 1.1.1.6 christos enum bptype type = bp_breakpoint;
825 1.1 christos int access_type = hw_write;
826 1.1 christos PyObject *internal = NULL;
827 1.1 christos PyObject *temporary = NULL;
828 1.1.1.6 christos PyObject *lineobj = NULL;;
829 1.1 christos int internal_bp = 0;
830 1.1 christos int temporary_bp = 0;
831 1.1.1.6 christos gdb::unique_xmalloc_ptr<char> line;
832 1.1.1.6 christos char *label = NULL;
833 1.1.1.6 christos char *source = NULL;
834 1.1.1.6 christos char *function = NULL;
835 1.1.1.6 christos PyObject * qualified = NULL;
836 1.1 christos
837 1.1.1.6 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
838 1.1.1.5 christos &spec, &type, &access_type,
839 1.1.1.6 christos &internal,
840 1.1.1.6 christos &temporary, &source,
841 1.1.1.6 christos &function, &label, &lineobj,
842 1.1.1.6 christos &qualified))
843 1.1 christos return -1;
844 1.1 christos
845 1.1.1.6 christos
846 1.1.1.6 christos if (lineobj != NULL)
847 1.1.1.6 christos {
848 1.1.1.8 christos if (PyLong_Check (lineobj))
849 1.1.1.8 christos line = xstrprintf ("%ld", PyLong_AsLong (lineobj));
850 1.1.1.8 christos else if (PyUnicode_Check (lineobj))
851 1.1.1.6 christos line = python_string_to_host_string (lineobj);
852 1.1.1.6 christos else
853 1.1.1.6 christos {
854 1.1.1.6 christos PyErr_SetString (PyExc_RuntimeError,
855 1.1.1.6 christos _("Line keyword should be an integer or a string. "));
856 1.1.1.6 christos return -1;
857 1.1.1.6 christos }
858 1.1.1.6 christos }
859 1.1.1.6 christos
860 1.1 christos if (internal)
861 1.1 christos {
862 1.1 christos internal_bp = PyObject_IsTrue (internal);
863 1.1 christos if (internal_bp == -1)
864 1.1 christos return -1;
865 1.1 christos }
866 1.1 christos
867 1.1 christos if (temporary != NULL)
868 1.1 christos {
869 1.1 christos temporary_bp = PyObject_IsTrue (temporary);
870 1.1 christos if (temporary_bp == -1)
871 1.1 christos return -1;
872 1.1 christos }
873 1.1 christos
874 1.1.1.6 christos if (bppy_init_validate_args (spec, source, function, label, line.get (),
875 1.1.1.6 christos type) == -1)
876 1.1.1.6 christos return -1;
877 1.1.1.6 christos
878 1.1 christos bppy_pending_object = (gdbpy_breakpoint_object *) self;
879 1.1 christos bppy_pending_object->number = -1;
880 1.1 christos bppy_pending_object->bp = NULL;
881 1.1 christos
882 1.1.1.7 christos try
883 1.1 christos {
884 1.1 christos switch (type)
885 1.1 christos {
886 1.1 christos case bp_breakpoint:
887 1.1.1.8 christos case bp_hardware_breakpoint:
888 1.1 christos {
889 1.1.1.8 christos location_spec_up locspec;
890 1.1.1.6 christos symbol_name_match_type func_name_match_type
891 1.1.1.6 christos = (qualified != NULL && PyObject_IsTrue (qualified)
892 1.1.1.6 christos ? symbol_name_match_type::FULL
893 1.1.1.6 christos : symbol_name_match_type::WILD);
894 1.1.1.6 christos
895 1.1.1.6 christos if (spec != NULL)
896 1.1.1.6 christos {
897 1.1.1.6 christos gdb::unique_xmalloc_ptr<char>
898 1.1.1.6 christos copy_holder (xstrdup (skip_spaces (spec)));
899 1.1.1.6 christos const char *copy = copy_holder.get ();
900 1.1.1.6 christos
901 1.1.1.8 christos locspec = string_to_location_spec (©,
902 1.1.1.8 christos current_language,
903 1.1.1.8 christos func_name_match_type);
904 1.1.1.6 christos }
905 1.1.1.6 christos else
906 1.1.1.6 christos {
907 1.1.1.8 christos std::unique_ptr<explicit_location_spec> explicit_loc
908 1.1.1.8 christos (new explicit_location_spec ());
909 1.1.1.6 christos
910 1.1.1.8 christos explicit_loc->source_filename
911 1.1.1.8 christos = source != nullptr ? xstrdup (source) : nullptr;
912 1.1.1.8 christos explicit_loc->function_name
913 1.1.1.8 christos = function != nullptr ? xstrdup (function) : nullptr;
914 1.1.1.8 christos explicit_loc->label_name
915 1.1.1.8 christos = label != nullptr ? xstrdup (label) : nullptr;
916 1.1.1.6 christos
917 1.1.1.6 christos if (line != NULL)
918 1.1.1.8 christos explicit_loc->line_offset
919 1.1.1.8 christos = linespec_parse_line_offset (line.get ());
920 1.1.1.6 christos
921 1.1.1.8 christos explicit_loc->func_name_match_type = func_name_match_type;
922 1.1.1.6 christos
923 1.1.1.8 christos locspec.reset (explicit_loc.release ());
924 1.1.1.6 christos }
925 1.1.1.6 christos
926 1.1.1.8 christos const struct breakpoint_ops *ops
927 1.1.1.8 christos = breakpoint_ops_for_location_spec (locspec.get (), false);
928 1.1.1.7 christos
929 1.1.1.8 christos create_breakpoint (gdbpy_enter::get_gdbarch (),
930 1.1.1.8 christos locspec.get (), NULL, -1, NULL, false,
931 1.1 christos 0,
932 1.1.1.8 christos temporary_bp, type,
933 1.1 christos 0,
934 1.1 christos AUTO_BOOLEAN_TRUE,
935 1.1.1.7 christos ops,
936 1.1 christos 0, 1, internal_bp, 0);
937 1.1 christos break;
938 1.1 christos }
939 1.1.1.6 christos case bp_watchpoint:
940 1.1 christos {
941 1.1.1.8 christos spec = skip_spaces (spec);
942 1.1.1.6 christos
943 1.1 christos if (access_type == hw_write)
944 1.1.1.8 christos watch_command_wrapper (spec, 0, internal_bp);
945 1.1 christos else if (access_type == hw_access)
946 1.1.1.8 christos awatch_command_wrapper (spec, 0, internal_bp);
947 1.1 christos else if (access_type == hw_read)
948 1.1.1.8 christos rwatch_command_wrapper (spec, 0, internal_bp);
949 1.1 christos else
950 1.1 christos error(_("Cannot understand watchpoint access type."));
951 1.1 christos break;
952 1.1 christos }
953 1.1.1.8 christos case bp_catchpoint:
954 1.1.1.8 christos error (_("BP_CATCHPOINT not supported"));
955 1.1 christos default:
956 1.1 christos error(_("Do not understand breakpoint type to set."));
957 1.1 christos }
958 1.1 christos }
959 1.1.1.7 christos catch (const gdb_exception &except)
960 1.1 christos {
961 1.1.1.4 christos bppy_pending_object = NULL;
962 1.1.1.6 christos gdbpy_convert_exception (except);
963 1.1 christos return -1;
964 1.1 christos }
965 1.1 christos
966 1.1 christos BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
967 1.1 christos return 0;
968 1.1 christos }
969 1.1 christos
970 1.1.1.8 christos /* Append to LIST the breakpoint Python object associated to B.
971 1.1.1.8 christos
972 1.1.1.8 christos Return true on success. Return false on failure, with the Python error
973 1.1.1.8 christos indicator set. */
974 1.1 christos
975 1.1.1.7 christos static bool
976 1.1.1.7 christos build_bp_list (struct breakpoint *b, PyObject *list)
977 1.1 christos {
978 1.1 christos PyObject *bp = (PyObject *) b->py_bp_object;
979 1.1 christos
980 1.1 christos /* Not all breakpoints will have a companion Python object.
981 1.1 christos Only breakpoints that were created via bppy_new, or
982 1.1 christos breakpoints that were created externally and are tracked by
983 1.1 christos the Python Scripting API. */
984 1.1.1.8 christos if (bp == nullptr)
985 1.1.1.7 christos return true;
986 1.1 christos
987 1.1.1.8 christos return PyList_Append (list, bp) == 0;
988 1.1.1.8 christos }
989 1.1.1.8 christos
990 1.1.1.8 christos /* See python-internal.h. */
991 1.1.1.8 christos
992 1.1.1.8 christos bool
993 1.1.1.8 christos gdbpy_breakpoint_init_breakpoint_type ()
994 1.1.1.8 christos {
995 1.1.1.8 christos if (breakpoint_object_type.tp_new == nullptr)
996 1.1.1.8 christos {
997 1.1.1.8 christos breakpoint_object_type.tp_new = PyType_GenericNew;
998 1.1.1.8 christos if (PyType_Ready (&breakpoint_object_type) < 0)
999 1.1.1.8 christos {
1000 1.1.1.8 christos /* Reset tp_new back to nullptr so future calls to this function
1001 1.1.1.8 christos will try calling PyType_Ready again. */
1002 1.1.1.8 christos breakpoint_object_type.tp_new = nullptr;
1003 1.1.1.8 christos return false;
1004 1.1.1.8 christos }
1005 1.1.1.8 christos }
1006 1.1.1.8 christos
1007 1.1.1.8 christos return true;
1008 1.1 christos }
1009 1.1 christos
1010 1.1 christos /* Static function to return a tuple holding all breakpoints. */
1011 1.1 christos
1012 1.1 christos PyObject *
1013 1.1 christos gdbpy_breakpoints (PyObject *self, PyObject *args)
1014 1.1 christos {
1015 1.1 christos if (bppy_live == 0)
1016 1.1.1.4 christos return PyTuple_New (0);
1017 1.1 christos
1018 1.1.1.5 christos gdbpy_ref<> list (PyList_New (0));
1019 1.1.1.5 christos if (list == NULL)
1020 1.1 christos return NULL;
1021 1.1 christos
1022 1.1.1.8 christos /* If build_bp_list returns false, it signals an error condition. In that
1023 1.1.1.8 christos case abandon building the list and return nullptr. */
1024 1.1.1.8 christos for (breakpoint *bp : all_breakpoints ())
1025 1.1.1.8 christos if (!build_bp_list (bp, list.get ()))
1026 1.1.1.8 christos return nullptr;
1027 1.1 christos
1028 1.1.1.5 christos return PyList_AsTuple (list.get ());
1029 1.1 christos }
1030 1.1 christos
1031 1.1 christos /* Call the "stop" method (if implemented) in the breakpoint
1032 1.1 christos class. If the method returns True, the inferior will be
1033 1.1 christos stopped at the breakpoint. Otherwise the inferior will be
1034 1.1 christos allowed to continue. */
1035 1.1 christos
1036 1.1.1.2 christos enum ext_lang_bp_stop
1037 1.1.1.2 christos gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
1038 1.1.1.2 christos struct breakpoint *b)
1039 1.1 christos {
1040 1.1.1.2 christos int stop;
1041 1.1.1.2 christos struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
1042 1.1 christos PyObject *py_bp = (PyObject *) bp_obj;
1043 1.1.1.2 christos
1044 1.1.1.2 christos if (bp_obj == NULL)
1045 1.1.1.2 christos return EXT_LANG_BP_STOP_UNSET;
1046 1.1.1.2 christos
1047 1.1.1.2 christos stop = -1;
1048 1.1.1.5 christos
1049 1.1.1.8 christos gdbpy_enter enter_py (b->gdbarch);
1050 1.1 christos
1051 1.1 christos if (bp_obj->is_finish_bp)
1052 1.1 christos bpfinishpy_pre_stop_hook (bp_obj);
1053 1.1 christos
1054 1.1 christos if (PyObject_HasAttrString (py_bp, stop_func))
1055 1.1 christos {
1056 1.1.1.5 christos gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
1057 1.1 christos
1058 1.1.1.2 christos stop = 1;
1059 1.1.1.5 christos if (result != NULL)
1060 1.1 christos {
1061 1.1.1.5 christos int evaluate = PyObject_IsTrue (result.get ());
1062 1.1 christos
1063 1.1 christos if (evaluate == -1)
1064 1.1 christos gdbpy_print_stack ();
1065 1.1 christos
1066 1.1 christos /* If the "stop" function returns False that means
1067 1.1 christos the Python breakpoint wants GDB to continue. */
1068 1.1 christos if (! evaluate)
1069 1.1 christos stop = 0;
1070 1.1 christos }
1071 1.1 christos else
1072 1.1 christos gdbpy_print_stack ();
1073 1.1 christos }
1074 1.1 christos
1075 1.1 christos if (bp_obj->is_finish_bp)
1076 1.1 christos bpfinishpy_post_stop_hook (bp_obj);
1077 1.1 christos
1078 1.1.1.2 christos if (stop < 0)
1079 1.1.1.2 christos return EXT_LANG_BP_STOP_UNSET;
1080 1.1.1.2 christos return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
1081 1.1 christos }
1082 1.1 christos
1083 1.1 christos /* Checks if the "stop" method exists in this breakpoint.
1084 1.1 christos Used by condition_command to ensure mutual exclusion of breakpoint
1085 1.1 christos conditions. */
1086 1.1 christos
1087 1.1 christos int
1088 1.1.1.2 christos gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
1089 1.1.1.2 christos struct breakpoint *b)
1090 1.1 christos {
1091 1.1.1.2 christos PyObject *py_bp;
1092 1.1.1.2 christos
1093 1.1.1.2 christos if (b->py_bp_object == NULL)
1094 1.1.1.2 christos return 0;
1095 1.1.1.2 christos
1096 1.1.1.2 christos py_bp = (PyObject *) b->py_bp_object;
1097 1.1 christos
1098 1.1.1.8 christos gdbpy_enter enter_py (b->gdbarch);
1099 1.1.1.5 christos return PyObject_HasAttrString (py_bp, stop_func);
1100 1.1 christos }
1101 1.1 christos
1102 1.1 christos
1103 1.1 christos
1105 1.1 christos /* Event callback functions. */
1106 1.1 christos
1107 1.1 christos /* Callback that is used when a breakpoint is created. This function
1108 1.1 christos will create a new Python breakpoint object. */
1109 1.1 christos static void
1110 1.1 christos gdbpy_breakpoint_created (struct breakpoint *bp)
1111 1.1.1.8 christos {
1112 1.1.1.8 christos PYBP_SCOPED_DEBUG_ENTER_EXIT;
1113 1.1 christos
1114 1.1 christos gdbpy_breakpoint_object *newbp;
1115 1.1.1.4 christos
1116 1.1.1.8 christos if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
1117 1.1.1.8 christos {
1118 1.1.1.8 christos pybp_debug_printf ("not attaching python object to this breakpoint");
1119 1.1.1.8 christos return;
1120 1.1 christos }
1121 1.1 christos
1122 1.1.1.8 christos if (bp->type != bp_breakpoint
1123 1.1 christos && bp->type != bp_hardware_breakpoint
1124 1.1 christos && bp->type != bp_watchpoint
1125 1.1 christos && bp->type != bp_hardware_watchpoint
1126 1.1.1.8 christos && bp->type != bp_read_watchpoint
1127 1.1.1.8 christos && bp->type != bp_access_watchpoint
1128 1.1.1.8 christos && bp->type != bp_catchpoint)
1129 1.1.1.8 christos {
1130 1.1.1.8 christos pybp_debug_printf ("is not a breakpoint or watchpoint");
1131 1.1.1.8 christos return;
1132 1.1 christos }
1133 1.1.1.8 christos
1134 1.1 christos gdbpy_enter enter_py (bp->gdbarch);
1135 1.1 christos
1136 1.1 christos if (bppy_pending_object)
1137 1.1 christos {
1138 1.1.1.7 christos newbp = bppy_pending_object;
1139 1.1 christos Py_INCREF (newbp);
1140 1.1.1.8 christos bppy_pending_object = NULL;
1141 1.1 christos pybp_debug_printf ("attaching existing breakpoint object");
1142 1.1 christos }
1143 1.1.1.8 christos else
1144 1.1.1.8 christos {
1145 1.1.1.8 christos newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
1146 1.1.1.8 christos pybp_debug_printf ("attaching new breakpoint object");
1147 1.1 christos }
1148 1.1 christos if (newbp)
1149 1.1 christos {
1150 1.1 christos newbp->number = bp->number;
1151 1.1 christos newbp->bp = bp;
1152 1.1 christos newbp->bp->py_bp_object = newbp;
1153 1.1 christos newbp->is_finish_bp = 0;
1154 1.1 christos ++bppy_live;
1155 1.1 christos }
1156 1.1 christos else
1157 1.1 christos {
1158 1.1 christos PyErr_SetString (PyExc_RuntimeError,
1159 1.1 christos _("Error while creating breakpoint from GDB."));
1160 1.1 christos gdbpy_print_stack ();
1161 1.1 christos }
1162 1.1.1.4 christos
1163 1.1.1.4 christos if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1164 1.1.1.4 christos {
1165 1.1.1.4 christos if (evpy_emit_event ((PyObject *) newbp,
1166 1.1.1.4 christos gdb_py_events.breakpoint_created) < 0)
1167 1.1.1.4 christos gdbpy_print_stack ();
1168 1.1 christos }
1169 1.1 christos }
1170 1.1 christos
1171 1.1 christos /* Callback that is used when a breakpoint is deleted. This will
1172 1.1 christos invalidate the corresponding Python object. */
1173 1.1 christos static void
1174 1.1 christos gdbpy_breakpoint_deleted (struct breakpoint *b)
1175 1.1.1.8 christos {
1176 1.1.1.8 christos PYBP_SCOPED_DEBUG_ENTER_EXIT;
1177 1.1 christos
1178 1.1 christos int num = b->number;
1179 1.1 christos struct breakpoint *bp = NULL;
1180 1.1 christos
1181 1.1 christos bp = get_breakpoint (num);
1182 1.1 christos if (bp)
1183 1.1.1.8 christos {
1184 1.1.1.7 christos gdbpy_enter enter_py (b->gdbarch);
1185 1.1.1.5 christos
1186 1.1.1.5 christos gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1187 1.1 christos if (bp_obj != NULL)
1188 1.1.1.4 christos {
1189 1.1.1.4 christos if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1190 1.1.1.5 christos {
1191 1.1.1.4 christos if (evpy_emit_event ((PyObject *) bp_obj.get (),
1192 1.1.1.4 christos gdb_py_events.breakpoint_deleted) < 0)
1193 1.1.1.4 christos gdbpy_print_stack ();
1194 1.1.1.4 christos }
1195 1.1 christos
1196 1.1 christos bp_obj->bp = NULL;
1197 1.1 christos --bppy_live;
1198 1.1 christos }
1199 1.1 christos }
1200 1.1 christos }
1201 1.1.1.4 christos
1202 1.1.1.4 christos /* Callback that is used when a breakpoint is modified. */
1203 1.1.1.4 christos
1204 1.1.1.4 christos static void
1205 1.1.1.4 christos gdbpy_breakpoint_modified (struct breakpoint *b)
1206 1.1.1.8 christos {
1207 1.1.1.8 christos PYBP_SCOPED_DEBUG_ENTER_EXIT;
1208 1.1.1.4 christos
1209 1.1.1.4 christos int num = b->number;
1210 1.1.1.4 christos struct breakpoint *bp = NULL;
1211 1.1.1.4 christos
1212 1.1.1.4 christos bp = get_breakpoint (num);
1213 1.1.1.4 christos if (bp)
1214 1.1.1.8 christos {
1215 1.1.1.7 christos gdbpy_enter enter_py (b->gdbarch);
1216 1.1.1.4 christos
1217 1.1.1.4 christos PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1218 1.1.1.4 christos if (bp_obj)
1219 1.1.1.4 christos {
1220 1.1.1.4 christos if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1221 1.1.1.4 christos {
1222 1.1.1.4 christos if (evpy_emit_event (bp_obj,
1223 1.1.1.4 christos gdb_py_events.breakpoint_modified) < 0)
1224 1.1.1.4 christos gdbpy_print_stack ();
1225 1.1.1.4 christos }
1226 1.1.1.4 christos }
1227 1.1.1.4 christos }
1228 1.1.1.4 christos }
1229 1.1 christos
1230 1.1 christos
1231 1.1 christos
1233 1.1 christos /* Initialize the Python breakpoint code. */
1234 1.1 christos int
1235 1.1 christos gdbpy_initialize_breakpoints (void)
1236 1.1 christos {
1237 1.1.1.8 christos int i;
1238 1.1 christos
1239 1.1 christos if (!gdbpy_breakpoint_init_breakpoint_type ())
1240 1.1 christos return -1;
1241 1.1 christos
1242 1.1 christos if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1243 1.1 christos (PyObject *) &breakpoint_object_type) < 0)
1244 1.1.1.8 christos return -1;
1245 1.1.1.8 christos
1246 1.1.1.8 christos gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created,
1247 1.1.1.8 christos "py-breakpoint");
1248 1.1.1.8 christos gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted,
1249 1.1.1.8 christos "py-breakpoint");
1250 1.1 christos gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified,
1251 1.1 christos "py-breakpoint");
1252 1.1 christos
1253 1.1 christos /* Add breakpoint types constants. */
1254 1.1.1.7 christos for (i = 0; pybp_codes[i].name; ++i)
1255 1.1 christos {
1256 1.1 christos if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
1257 1.1 christos pybp_codes[i].code) < 0)
1258 1.1 christos return -1;
1259 1.1 christos }
1260 1.1 christos
1261 1.1 christos /* Add watchpoint types constants. */
1262 1.1.1.7 christos for (i = 0; pybp_watch_types[i].name; ++i)
1263 1.1 christos {
1264 1.1 christos if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
1265 1.1 christos pybp_watch_types[i].code) < 0)
1266 1.1 christos return -1;
1267 1.1 christos }
1268 1.1 christos
1269 1.1 christos return 0;
1270 1.1.1.8 christos }
1271 1.1.1.8 christos
1272 1.1.1.8 christos /* Initialize the Python BreakpointLocation code. */
1273 1.1.1.8 christos
1274 1.1.1.8 christos int
1275 1.1.1.8 christos gdbpy_initialize_breakpoint_locations ()
1276 1.1.1.8 christos {
1277 1.1.1.8 christos if (PyType_Ready (&breakpoint_location_object_type) < 0)
1278 1.1.1.8 christos return -1;
1279 1.1.1.8 christos
1280 1.1.1.8 christos if (gdb_pymodule_addobject (gdb_module, "BreakpointLocation",
1281 1.1.1.8 christos (PyObject *) &breakpoint_location_object_type)
1282 1.1.1.8 christos < 0)
1283 1.1.1.8 christos return -1;
1284 1.1.1.8 christos return 0;
1285 1.1 christos }
1286 1.1 christos
1287 1.1 christos
1288 1.1 christos
1290 1.1 christos /* Helper function that overrides this Python object's
1291 1.1 christos PyObject_GenericSetAttr to allow extra validation of the attribute
1292 1.1 christos being set. */
1293 1.1 christos
1294 1.1 christos static int
1295 1.1.1.5 christos local_setattro (PyObject *self, PyObject *name, PyObject *v)
1296 1.1 christos {
1297 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1298 1.1 christos gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
1299 1.1 christos
1300 1.1 christos if (attr == NULL)
1301 1.1.1.2 christos return -1;
1302 1.1.1.2 christos
1303 1.1.1.5 christos /* If the attribute trying to be set is the "stop" method,
1304 1.1.1.2 christos but we already have a condition set in the CLI or other extension
1305 1.1.1.2 christos language, disallow this operation. */
1306 1.1.1.2 christos if (strcmp (attr.get (), stop_func) == 0)
1307 1.1.1.2 christos {
1308 1.1.1.2 christos const struct extension_language_defn *extlang = NULL;
1309 1.1.1.2 christos
1310 1.1.1.2 christos if (obj->bp->cond_string != NULL)
1311 1.1.1.2 christos extlang = get_ext_lang_defn (EXT_LANG_GDB);
1312 1.1.1.2 christos if (extlang == NULL)
1313 1.1.1.6 christos extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1314 1.1.1.6 christos if (extlang != NULL)
1315 1.1.1.6 christos {
1316 1.1.1.6 christos std::string error_text
1317 1.1.1.6 christos = string_printf (_("Only one stop condition allowed. There is"
1318 1.1.1.6 christos " currently a %s stop condition defined for"
1319 1.1.1.2 christos " this breakpoint."),
1320 1.1.1.2 christos ext_lang_capitalized_name (extlang));
1321 1.1 christos PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
1322 1.1 christos return -1;
1323 1.1.1.6 christos }
1324 1.1 christos }
1325 1.1 christos
1326 1.1.1.5 christos return PyObject_GenericSetAttr (self, name, v);
1327 1.1 christos }
1328 1.1 christos
1329 1.1 christos static gdb_PyGetSetDef breakpoint_object_getset[] = {
1330 1.1 christos { "enabled", bppy_get_enabled, bppy_set_enabled,
1331 1.1 christos "Boolean telling whether the breakpoint is enabled.", NULL },
1332 1.1 christos { "silent", bppy_get_silent, bppy_set_silent,
1333 1.1 christos "Boolean telling whether the breakpoint is silent.", NULL },
1334 1.1 christos { "thread", bppy_get_thread, bppy_set_thread,
1335 1.1 christos "Thread ID for the breakpoint.\n\
1336 1.1 christos If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1337 1.1 christos If the value is None, then this breakpoint is not thread-specific.\n\
1338 1.1 christos No other type of value can be used.", NULL },
1339 1.1 christos { "task", bppy_get_task, bppy_set_task,
1340 1.1 christos "Thread ID for the breakpoint.\n\
1341 1.1 christos If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1342 1.1 christos If the value is None, then this breakpoint is not task-specific.\n\
1343 1.1 christos No other type of value can be used.", NULL },
1344 1.1 christos { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1345 1.1 christos "Number of times this breakpoint should be automatically continued.",
1346 1.1 christos NULL },
1347 1.1 christos { "number", bppy_get_number, NULL,
1348 1.1 christos "Breakpoint's number assigned by GDB.", NULL },
1349 1.1 christos { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1350 1.1 christos "Number of times the breakpoint has been hit.\n\
1351 1.1 christos Can be set to zero to clear the count. No other value is valid\n\
1352 1.1 christos when setting this property.", NULL },
1353 1.1 christos { "location", bppy_get_location, NULL,
1354 1.1 christos "Location of the breakpoint, as specified by the user.", NULL},
1355 1.1 christos { "expression", bppy_get_expression, NULL,
1356 1.1 christos "Expression of the breakpoint, as specified by the user.", NULL},
1357 1.1.1.6 christos { "condition", bppy_get_condition, bppy_set_condition,
1358 1.1 christos "Condition of the breakpoint, as specified by the user,\
1359 1.1 christos or None if no condition set."},
1360 1.1 christos { "commands", bppy_get_commands, bppy_set_commands,
1361 1.1 christos "Commands of the breakpoint, as specified by the user."},
1362 1.1 christos { "type", bppy_get_type, NULL,
1363 1.1 christos "Type of breakpoint."},
1364 1.1 christos { "visible", bppy_get_visibility, NULL,
1365 1.1.1.4 christos "Whether the breakpoint is visible to the user."},
1366 1.1.1.4 christos { "temporary", bppy_get_temporary, NULL,
1367 1.1.1.8 christos "Whether this breakpoint is a temporary breakpoint."},
1368 1.1.1.8 christos { "pending", bppy_get_pending, NULL,
1369 1.1 christos "Whether this breakpoint is a pending breakpoint."},
1370 1.1 christos { "locations", bppy_get_locations, NULL,
1371 1.1 christos "Get locations where this breakpoint was set"},
1372 1.1 christos { NULL } /* Sentinel. */
1373 1.1 christos };
1374 1.1 christos
1375 1.1 christos static PyMethodDef breakpoint_object_methods[] =
1376 1.1 christos {
1377 1.1 christos { "is_valid", bppy_is_valid, METH_NOARGS,
1378 1.1 christos "Return true if this breakpoint is valid, false if not." },
1379 1.1 christos { "delete", bppy_delete_breakpoint, METH_NOARGS,
1380 1.1 christos "Delete the underlying GDB breakpoint." },
1381 1.1 christos { NULL } /* Sentinel. */
1382 1.1 christos };
1383 1.1 christos
1384 1.1 christos PyTypeObject breakpoint_object_type =
1385 1.1 christos {
1386 1.1 christos PyVarObject_HEAD_INIT (NULL, 0)
1387 1.1 christos "gdb.Breakpoint", /*tp_name*/
1388 1.1 christos sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1389 1.1 christos 0, /*tp_itemsize*/
1390 1.1 christos 0, /*tp_dealloc*/
1391 1.1 christos 0, /*tp_print*/
1392 1.1 christos 0, /*tp_getattr*/
1393 1.1 christos 0, /*tp_setattr*/
1394 1.1 christos 0, /*tp_compare*/
1395 1.1 christos 0, /*tp_repr*/
1396 1.1 christos 0, /*tp_as_number*/
1397 1.1 christos 0, /*tp_as_sequence*/
1398 1.1 christos 0, /*tp_as_mapping*/
1399 1.1 christos 0, /*tp_hash */
1400 1.1 christos 0, /*tp_call*/
1401 1.1 christos 0, /*tp_str*/
1402 1.1 christos 0, /*tp_getattro*/
1403 1.1 christos (setattrofunc)local_setattro, /*tp_setattro */
1404 1.1 christos 0, /*tp_as_buffer*/
1405 1.1 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1406 1.1 christos "GDB breakpoint object", /* tp_doc */
1407 1.1 christos 0, /* tp_traverse */
1408 1.1 christos 0, /* tp_clear */
1409 1.1 christos 0, /* tp_richcompare */
1410 1.1 christos 0, /* tp_weaklistoffset */
1411 1.1 christos 0, /* tp_iter */
1412 1.1 christos 0, /* tp_iternext */
1413 1.1 christos breakpoint_object_methods, /* tp_methods */
1414 1.1 christos 0, /* tp_members */
1415 1.1 christos breakpoint_object_getset, /* tp_getset */
1416 1.1 christos 0, /* tp_base */
1417 1.1 christos 0, /* tp_dict */
1418 1.1 christos 0, /* tp_descr_get */
1419 1.1 christos 0, /* tp_descr_set */
1420 1.1 christos 0, /* tp_dictoffset */
1421 1.1.1.8 christos bppy_init, /* tp_init */
1422 1.1.1.8 christos 0, /* tp_alloc */
1423 1.1.1.8 christos };
1424 1.1.1.8 christos
1425 1.1.1.8 christos void _initialize_py_breakpoint ();
1426 1.1.1.8 christos void
1427 1.1.1.8 christos _initialize_py_breakpoint ()
1428 1.1.1.8 christos {
1429 1.1.1.8 christos add_setshow_boolean_cmd
1430 1.1.1.8 christos ("py-breakpoint", class_maintenance, &pybp_debug,
1431 1.1.1.8 christos _("Set Python breakpoint debugging."),
1432 1.1.1.8 christos _("Show Python breakpoint debugging."),
1433 1.1.1.8 christos _("When on, Python breakpoint debugging is enabled."),
1434 1.1.1.8 christos NULL,
1435 1.1.1.8 christos show_pybp_debug,
1436 1.1.1.8 christos &setdebuglist, &showdebuglist);
1437 1.1.1.8 christos }
1438 1.1.1.8 christos
1439 1.1.1.8 christos /* Python function to set the enabled state of a breakpoint location. */
1440 1.1.1.8 christos
1441 1.1.1.8 christos static int
1442 1.1.1.8 christos bplocpy_set_enabled (PyObject *py_self, PyObject *newvalue, void *closure)
1443 1.1.1.8 christos {
1444 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1445 1.1.1.8 christos BPPY_SET_REQUIRE_VALID (self->owner);
1446 1.1.1.8 christos BPLOCPY_SET_REQUIRE_VALID (self->owner, self);
1447 1.1.1.8 christos
1448 1.1.1.8 christos if (newvalue == nullptr)
1449 1.1.1.8 christos {
1450 1.1.1.8 christos PyErr_SetString (PyExc_TypeError,
1451 1.1.1.8 christos _("Cannot delete 'enabled' attribute."));
1452 1.1.1.8 christos return -1;
1453 1.1.1.8 christos }
1454 1.1.1.8 christos else if (!PyBool_Check (newvalue))
1455 1.1.1.8 christos {
1456 1.1.1.8 christos PyErr_SetString (PyExc_TypeError,
1457 1.1.1.8 christos _("The value of 'enabled' must be a boolean."));
1458 1.1.1.8 christos return -1;
1459 1.1.1.8 christos }
1460 1.1.1.8 christos
1461 1.1.1.8 christos int cmp = PyObject_IsTrue (newvalue);
1462 1.1.1.8 christos if (cmp < 0)
1463 1.1.1.8 christos return -1;
1464 1.1.1.8 christos
1465 1.1.1.8 christos try
1466 1.1.1.8 christos {
1467 1.1.1.8 christos enable_disable_bp_location (self->bp_loc, cmp == 1);
1468 1.1.1.8 christos }
1469 1.1.1.8 christos catch (const gdb_exception &except)
1470 1.1.1.8 christos {
1471 1.1.1.8 christos GDB_PY_SET_HANDLE_EXCEPTION (except);
1472 1.1.1.8 christos }
1473 1.1.1.8 christos return 0;
1474 1.1.1.8 christos }
1475 1.1.1.8 christos
1476 1.1.1.8 christos /* Python function to test whether or not the breakpoint location is enabled. */
1477 1.1.1.8 christos
1478 1.1.1.8 christos static PyObject *
1479 1.1.1.8 christos bplocpy_get_enabled (PyObject *py_self, void *closure)
1480 1.1.1.8 christos {
1481 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1482 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1483 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1484 1.1.1.8 christos
1485 1.1.1.8 christos if (self->bp_loc->enabled)
1486 1.1.1.8 christos Py_RETURN_TRUE;
1487 1.1.1.8 christos else
1488 1.1.1.8 christos Py_RETURN_FALSE;
1489 1.1.1.8 christos }
1490 1.1.1.8 christos
1491 1.1.1.8 christos /* Python function to get address of breakpoint location. */
1492 1.1.1.8 christos
1493 1.1.1.8 christos static PyObject *
1494 1.1.1.8 christos bplocpy_get_address (PyObject *py_self, void *closure)
1495 1.1.1.8 christos {
1496 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1497 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1498 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1499 1.1.1.8 christos return gdb_py_object_from_ulongest (self->bp_loc->address).release ();
1500 1.1.1.8 christos }
1501 1.1.1.8 christos
1502 1.1.1.8 christos /* Python function to get owner of breakpoint location, which
1503 1.1.1.8 christos is of type gdb.Breakpoint. */
1504 1.1.1.8 christos
1505 1.1.1.8 christos static PyObject *
1506 1.1.1.8 christos bplocpy_get_owner (PyObject *py_self, void *closure)
1507 1.1.1.8 christos {
1508 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1509 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1510 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1511 1.1.1.8 christos Py_INCREF (self->owner);
1512 1.1.1.8 christos return (PyObject *) self->owner;
1513 1.1.1.8 christos }
1514 1.1.1.8 christos
1515 1.1.1.8 christos /* Python function to get the source file name path and line number
1516 1.1.1.8 christos where this breakpoint location was set. */
1517 1.1.1.8 christos
1518 1.1.1.8 christos static PyObject *
1519 1.1.1.8 christos bplocpy_get_source_location (PyObject *py_self, void *closure)
1520 1.1.1.8 christos {
1521 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1522 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1523 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1524 1.1.1.8 christos if (self->bp_loc->symtab)
1525 1.1.1.8 christos {
1526 1.1.1.8 christos gdbpy_ref<> tup (PyTuple_New (2));
1527 1.1.1.8 christos if (tup == nullptr)
1528 1.1.1.8 christos return nullptr;
1529 1.1.1.8 christos /* symtab->filename is never NULL. */
1530 1.1.1.8 christos gdbpy_ref<> filename
1531 1.1.1.8 christos = host_string_to_python_string (self->bp_loc->symtab->filename);
1532 1.1.1.8 christos if (filename == nullptr)
1533 1.1.1.8 christos return nullptr;
1534 1.1.1.8 christos auto line = gdb_py_object_from_ulongest (self->bp_loc->line_number);
1535 1.1.1.8 christos if (line == nullptr)
1536 1.1.1.8 christos return nullptr;
1537 1.1.1.8 christos if (PyTuple_SetItem (tup.get (), 0, filename.release ()) == -1
1538 1.1.1.8 christos || PyTuple_SetItem (tup.get (), 1, line.release ()) == -1)
1539 1.1.1.8 christos return nullptr;
1540 1.1.1.8 christos return tup.release ();
1541 1.1.1.8 christos }
1542 1.1.1.8 christos else
1543 1.1.1.8 christos Py_RETURN_NONE;
1544 1.1.1.8 christos }
1545 1.1.1.8 christos
1546 1.1.1.8 christos /* Python function to get the function name of where this location was set. */
1547 1.1.1.8 christos
1548 1.1.1.8 christos static PyObject *
1549 1.1.1.8 christos bplocpy_get_function (PyObject *py_self, void *closure)
1550 1.1.1.8 christos {
1551 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1552 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1553 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1554 1.1.1.8 christos const auto fn_name = self->bp_loc->function_name.get ();
1555 1.1.1.8 christos if (fn_name != nullptr)
1556 1.1.1.8 christos return host_string_to_python_string (fn_name).release ();
1557 1.1.1.8 christos Py_RETURN_NONE;
1558 1.1.1.8 christos }
1559 1.1.1.8 christos
1560 1.1.1.8 christos static PyObject *
1561 1.1.1.8 christos bplocpy_get_thread_groups (PyObject *py_self, void *closure)
1562 1.1.1.8 christos {
1563 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1564 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1565 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1566 1.1.1.8 christos gdbpy_ref<> list (PyList_New (0));
1567 1.1.1.8 christos if (list == nullptr)
1568 1.1.1.8 christos return nullptr;
1569 1.1.1.8 christos for (inferior *inf : all_inferiors ())
1570 1.1.1.8 christos {
1571 1.1.1.8 christos if (inf->pspace == self->bp_loc->pspace)
1572 1.1.1.8 christos {
1573 1.1.1.8 christos gdbpy_ref<> num = gdb_py_object_from_ulongest (inf->num);
1574 1.1.1.8 christos if (num == nullptr)
1575 1.1.1.8 christos return nullptr;
1576 1.1.1.8 christos if (PyList_Append (list.get (), num.release ()) != 0)
1577 1.1.1.8 christos return nullptr;
1578 1.1.1.8 christos }
1579 1.1.1.8 christos }
1580 1.1.1.8 christos return list.release ();
1581 1.1.1.8 christos }
1582 1.1.1.8 christos
1583 1.1.1.8 christos static PyObject *
1584 1.1.1.8 christos bplocpy_get_fullname (PyObject *py_self, void *closure)
1585 1.1.1.8 christos {
1586 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1587 1.1.1.8 christos BPPY_REQUIRE_VALID (self->owner);
1588 1.1.1.8 christos BPLOCPY_REQUIRE_VALID (self->owner, self);
1589 1.1.1.8 christos const auto symtab = self->bp_loc->symtab;
1590 1.1.1.8 christos if (symtab != nullptr && symtab->fullname != nullptr)
1591 1.1.1.8 christos {
1592 1.1.1.8 christos gdbpy_ref<> fullname
1593 1.1.1.8 christos = host_string_to_python_string (symtab->fullname);
1594 1.1.1.8 christos return fullname.release ();
1595 1.1.1.8 christos }
1596 1.1.1.8 christos Py_RETURN_NONE;
1597 1.1.1.8 christos }
1598 1.1.1.8 christos
1599 1.1.1.8 christos /* De-allocation function to be called for the Python object. */
1600 1.1.1.8 christos
1601 1.1.1.8 christos static void
1602 1.1.1.8 christos bplocpy_dealloc (PyObject *py_self)
1603 1.1.1.8 christos {
1604 1.1.1.8 christos auto *self = (gdbpy_breakpoint_location_object *) py_self;
1605 1.1.1.8 christos bp_location_ref_ptr decrementing_ref {self->bp_loc};
1606 1.1.1.8 christos Py_XDECREF (self->owner);
1607 1.1.1.8 christos Py_TYPE (py_self)->tp_free (py_self);
1608 1.1.1.8 christos }
1609 1.1.1.8 christos
1610 1.1.1.8 christos /* Attribute get/set Python definitions. */
1611 1.1.1.8 christos
1612 1.1.1.8 christos static gdb_PyGetSetDef bp_location_object_getset[] = {
1613 1.1.1.8 christos { "enabled", bplocpy_get_enabled, bplocpy_set_enabled,
1614 1.1.1.8 christos "Boolean telling whether the breakpoint is enabled.", NULL },
1615 1.1.1.8 christos { "owner", bplocpy_get_owner, NULL,
1616 1.1.1.8 christos "Get the breakpoint owner object", NULL },
1617 1.1.1.8 christos { "address", bplocpy_get_address, NULL,
1618 1.1.1.8 christos "Get address of where this location was set", NULL},
1619 1.1.1.8 christos { "source", bplocpy_get_source_location, NULL,
1620 1.1.1.8 christos "Get file and line number of where this location was set", NULL},
1621 1.1.1.8 christos { "function", bplocpy_get_function, NULL,
1622 1.1.1.8 christos "Get function of where this location was set", NULL },
1623 1.1.1.8 christos { "fullname", bplocpy_get_fullname, NULL,
1624 1.1.1.8 christos "Get fullname of where this location was set", NULL },
1625 1.1.1.8 christos { "thread_groups", bplocpy_get_thread_groups, NULL,
1626 1.1.1.8 christos "Get thread groups where this location is in", NULL },
1627 1.1.1.8 christos { NULL } /* Sentinel. */
1628 1.1.1.8 christos };
1629 1.1.1.8 christos
1630 1.1.1.8 christos PyTypeObject breakpoint_location_object_type =
1631 1.1.1.8 christos {
1632 1.1.1.8 christos PyVarObject_HEAD_INIT (NULL, 0)
1633 1.1.1.8 christos "gdb.BreakpointLocation", /*tp_name*/
1634 1.1.1.8 christos sizeof (gdbpy_breakpoint_location_object), /*tp_basicsize*/
1635 1.1.1.8 christos 0, /*tp_itemsize*/
1636 1.1.1.8 christos bplocpy_dealloc, /*tp_dealloc*/
1637 1.1.1.8 christos 0, /*tp_print*/
1638 1.1.1.8 christos 0, /*tp_getattr*/
1639 1.1.1.8 christos 0, /*tp_setattr*/
1640 1.1.1.8 christos 0, /*tp_compare*/
1641 1.1.1.8 christos 0, /*tp_repr*/
1642 1.1.1.8 christos 0, /*tp_as_number*/
1643 1.1.1.8 christos 0, /*tp_as_sequence*/
1644 1.1.1.8 christos 0, /*tp_as_mapping*/
1645 1.1.1.8 christos 0, /*tp_hash */
1646 1.1.1.8 christos 0, /*tp_call*/
1647 1.1.1.8 christos 0, /*tp_str*/
1648 1.1.1.8 christos 0, /*tp_getattro*/
1649 1.1.1.8 christos 0, /*tp_setattro */
1650 1.1.1.8 christos 0, /*tp_as_buffer*/
1651 1.1.1.8 christos Py_TPFLAGS_DEFAULT, /*tp_flags*/
1652 1.1.1.8 christos "GDB breakpoint location object", /* tp_doc */
1653 1.1.1.8 christos 0, /* tp_traverse */
1654 1.1.1.8 christos 0, /* tp_clear */
1655 1.1.1.8 christos 0, /* tp_richcompare */
1656 1.1.1.8 christos 0, /* tp_weaklistoffset */
1657 1.1.1.8 christos 0, /* tp_iter */
1658 1.1.1.8 christos 0, /* tp_iternext */
1659 1.1.1.8 christos 0, /* tp_methods */
1660 1.1.1.8 christos 0, /* tp_members */
1661 1.1.1.8 christos bp_location_object_getset, /* tp_getset */
1662 1.1.1.8 christos 0, /* tp_base */
1663 1.1.1.8 christos 0, /* tp_dict */
1664 1.1.1.8 christos 0, /* tp_descr_get */
1665 1.1.1.8 christos 0, /* tp_descr_set */
1666 1.1.1.8 christos 0, /* tp_dictoffset */
1667 0, /* tp_init */
1668 0, /* tp_alloc */
1669 };
1670