Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / python / py-breakpoint.c
blob95782444c4a7d911172615f82ea9184625deae9c
1 /* Python interface to breakpoints
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "value.h"
22 #include "python-internal.h"
23 #include "python.h"
24 #include "charset.h"
25 #include "breakpoint.h"
26 #include "gdbcmd.h"
27 #include "gdbthread.h"
28 #include "observable.h"
29 #include "cli/cli-script.h"
30 #include "ada-lang.h"
31 #include "arch-utils.h"
32 #include "language.h"
33 #include "location.h"
34 #include "py-event.h"
35 #include "linespec.h"
36 #include "gdbsupport/common-utils.h"
38 extern PyTypeObject breakpoint_location_object_type
39 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("breakpoint_location_object");
41 struct gdbpy_breakpoint_location_object
43 PyObject_HEAD
45 /* An owning reference to the gdb breakpoint location object. */
46 bp_location *bp_loc;
48 /* An owning reference to the location's breakpoint owner. */
49 gdbpy_breakpoint_object *owner;
52 /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python
53 exception if they are not. */
54 #define BPLOCPY_REQUIRE_VALID(Breakpoint, Location) \
55 do { \
56 if ((Breakpoint)->bp != (Location)->bp_loc->owner) \
57 return PyErr_Format (PyExc_RuntimeError, \
58 _("Breakpoint location is invalid.")); \
59 } while (0)
61 /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python
62 exception if they are not. This macro is for use in setter functions. */
63 #define BPLOCPY_SET_REQUIRE_VALID(Breakpoint, Location) \
64 do { \
65 if ((Breakpoint)->bp != (Location)->bp_loc->owner) \
66 { \
67 PyErr_Format (PyExc_RuntimeError, \
68 _("Breakpoint location is invalid.")); \
69 return -1; \
70 } \
71 } while (0)
73 /* Debugging of Python breakpoints. */
75 static bool pybp_debug;
77 /* Implementation of "show debug py-breakpoint". */
79 static void
80 show_pybp_debug (struct ui_file *file, int from_tty,
81 struct cmd_list_element *c, const char *value)
83 gdb_printf (file, _("Python breakpoint debugging is %s.\n"), value);
86 /* Print a "py-breakpoint" debug statement. */
88 #define pybp_debug_printf(fmt, ...) \
89 debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__)
91 /* Print a "py-breakpoint" enter/exit debug statements. */
93 #define PYBP_SCOPED_DEBUG_ENTER_EXIT \
94 scoped_debug_enter_exit (pybp_debug, "py-breakpoint")
96 /* Number of live breakpoints. */
97 static int bppy_live;
99 /* Variables used to pass information between the Breakpoint
100 constructor and the breakpoint-created hook function. */
101 gdbpy_breakpoint_object *bppy_pending_object;
103 /* Function that is called when a Python condition is evaluated. */
104 static const char stop_func[] = "stop";
106 /* This is used to initialize various gdb.bp_* constants. */
107 struct pybp_code
109 /* The name. */
110 const char *name;
111 /* The code. */
112 int code;
115 /* Entries related to the type of user set breakpoints. */
116 static struct pybp_code pybp_codes[] =
118 { "BP_NONE", bp_none},
119 { "BP_BREAKPOINT", bp_breakpoint},
120 { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint},
121 { "BP_WATCHPOINT", bp_watchpoint},
122 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
123 { "BP_READ_WATCHPOINT", bp_read_watchpoint},
124 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
125 { "BP_CATCHPOINT", bp_catchpoint},
126 {NULL} /* Sentinel. */
129 /* Entries related to the type of watchpoint. */
130 static struct pybp_code pybp_watch_types[] =
132 { "WP_READ", hw_read},
133 { "WP_WRITE", hw_write},
134 { "WP_ACCESS", hw_access},
135 {NULL} /* Sentinel. */
138 /* Python function which checks the validity of a breakpoint object. */
139 static PyObject *
140 bppy_is_valid (PyObject *self, PyObject *args)
142 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
144 if (self_bp->bp)
145 Py_RETURN_TRUE;
146 Py_RETURN_FALSE;
149 /* Python function to test whether or not the breakpoint is enabled. */
150 static PyObject *
151 bppy_get_enabled (PyObject *self, void *closure)
153 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
155 BPPY_REQUIRE_VALID (self_bp);
156 if (! self_bp->bp)
157 Py_RETURN_FALSE;
158 if (self_bp->bp->enable_state == bp_enabled)
159 Py_RETURN_TRUE;
160 Py_RETURN_FALSE;
163 /* Python function to test whether or not the breakpoint is silent. */
164 static PyObject *
165 bppy_get_silent (PyObject *self, void *closure)
167 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
169 BPPY_REQUIRE_VALID (self_bp);
170 if (self_bp->bp->silent)
171 Py_RETURN_TRUE;
172 Py_RETURN_FALSE;
175 /* Python function to set the enabled state of a breakpoint. */
176 static int
177 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
179 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
180 int cmp;
182 BPPY_SET_REQUIRE_VALID (self_bp);
184 if (newvalue == NULL)
186 PyErr_SetString (PyExc_TypeError,
187 _("Cannot delete `enabled' attribute."));
189 return -1;
191 else if (! PyBool_Check (newvalue))
193 PyErr_SetString (PyExc_TypeError,
194 _("The value of `enabled' must be a boolean."));
195 return -1;
198 cmp = PyObject_IsTrue (newvalue);
199 if (cmp < 0)
200 return -1;
204 if (cmp == 1)
205 enable_breakpoint (self_bp->bp);
206 else
207 disable_breakpoint (self_bp->bp);
209 catch (const gdb_exception &except)
211 GDB_PY_SET_HANDLE_EXCEPTION (except);
214 return 0;
217 /* Python function to set the 'silent' state of a breakpoint. */
218 static int
219 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
221 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
222 int cmp;
224 BPPY_SET_REQUIRE_VALID (self_bp);
226 if (newvalue == NULL)
228 PyErr_SetString (PyExc_TypeError,
229 _("Cannot delete `silent' attribute."));
230 return -1;
232 else if (! PyBool_Check (newvalue))
234 PyErr_SetString (PyExc_TypeError,
235 _("The value of `silent' must be a boolean."));
236 return -1;
239 cmp = PyObject_IsTrue (newvalue);
240 if (cmp < 0)
241 return -1;
242 else
243 breakpoint_set_silent (self_bp->bp, cmp);
245 return 0;
248 /* Python function to set the thread of a breakpoint. */
249 static int
250 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
252 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
253 long id;
255 BPPY_SET_REQUIRE_VALID (self_bp);
257 if (newvalue == NULL)
259 PyErr_SetString (PyExc_TypeError,
260 _("Cannot delete `thread' attribute."));
261 return -1;
263 else if (PyLong_Check (newvalue))
265 if (! gdb_py_int_as_long (newvalue, &id))
266 return -1;
268 if (!valid_global_thread_id (id))
270 PyErr_SetString (PyExc_RuntimeError,
271 _("Invalid thread ID."));
272 return -1;
275 if (self_bp->bp->task != -1)
277 PyErr_SetString (PyExc_RuntimeError,
278 _("Cannot set both task and thread attributes."));
279 return -1;
282 else if (newvalue == Py_None)
283 id = -1;
284 else
286 PyErr_SetString (PyExc_TypeError,
287 _("The value of `thread' must be an integer or None."));
288 return -1;
291 if (self_bp->bp->inferior != -1 && id != -1)
293 PyErr_SetString (PyExc_RuntimeError,
294 _("Cannot have both 'thread' and 'inferior' "
295 "conditions on a breakpoint"));
296 return -1;
299 breakpoint_set_thread (self_bp->bp, id);
301 return 0;
304 /* Python function to set the inferior of a breakpoint. */
306 static int
307 bppy_set_inferior (PyObject *self, PyObject *newvalue, void *closure)
309 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
310 long id;
312 BPPY_SET_REQUIRE_VALID (self_bp);
314 if (newvalue == NULL)
316 PyErr_SetString (PyExc_TypeError,
317 _("Cannot delete 'inferior' attribute."));
318 return -1;
320 else if (PyLong_Check (newvalue))
322 if (!gdb_py_int_as_long (newvalue, &id))
323 return -1;
325 if (!valid_global_inferior_id (id))
327 PyErr_SetString (PyExc_RuntimeError,
328 _("Invalid inferior ID."));
329 return -1;
332 else if (newvalue == Py_None)
333 id = -1;
334 else
336 PyErr_SetString (PyExc_TypeError,
337 _("The value of 'inferior' must be an integer or None."));
338 return -1;
341 if (self_bp->bp->type != bp_breakpoint
342 && self_bp->bp->type != bp_hardware_breakpoint)
344 PyErr_SetString (PyExc_RuntimeError,
345 _("Cannot set 'inferior' attribute on a gdb.Breakpoint "
346 "of this type"));
347 return -1;
350 if (self_bp->bp->thread != -1 && id != -1)
352 PyErr_SetString (PyExc_RuntimeError,
353 _("Cannot have both 'thread' and 'inferior' conditions "
354 "on a breakpoint"));
355 return -1;
358 if (self_bp->bp->task != -1 && id != -1)
360 PyErr_SetString (PyExc_RuntimeError,
361 _("Cannot have both 'task' and 'inferior' conditions "
362 "on a breakpoint"));
363 return -1;
366 breakpoint_set_inferior (self_bp->bp, id);
368 return 0;
371 /* Python function to set the (Ada) task of a breakpoint. */
372 static int
373 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
375 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
376 long id;
377 int valid_id = 0;
379 BPPY_SET_REQUIRE_VALID (self_bp);
381 if (newvalue == NULL)
383 PyErr_SetString (PyExc_TypeError,
384 _("Cannot delete `task' attribute."));
385 return -1;
387 else if (PyLong_Check (newvalue))
389 if (! gdb_py_int_as_long (newvalue, &id))
390 return -1;
394 valid_id = valid_task_id (id);
396 catch (const gdb_exception &except)
398 GDB_PY_SET_HANDLE_EXCEPTION (except);
401 if (! valid_id)
403 PyErr_SetString (PyExc_RuntimeError,
404 _("Invalid task ID."));
405 return -1;
408 if (self_bp->bp->thread != -1)
410 PyErr_SetString (PyExc_RuntimeError,
411 _("Cannot set both task and thread attributes."));
412 return -1;
415 else if (newvalue == Py_None)
416 id = -1;
417 else
419 PyErr_SetString (PyExc_TypeError,
420 _("The value of `task' must be an integer or None."));
421 return -1;
424 breakpoint_set_task (self_bp->bp, id);
426 return 0;
429 /* Python function which deletes the underlying GDB breakpoint. This
430 triggers the breakpoint_deleted observer which will call
431 gdbpy_breakpoint_deleted; that function cleans up the Python
432 sections. */
434 static PyObject *
435 bppy_delete_breakpoint (PyObject *self, PyObject *args)
437 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
439 BPPY_REQUIRE_VALID (self_bp);
443 delete_breakpoint (self_bp->bp);
445 catch (const gdb_exception &except)
447 GDB_PY_HANDLE_EXCEPTION (except);
450 Py_RETURN_NONE;
454 /* Python function to set the ignore count of a breakpoint. */
455 static int
456 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
458 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
459 long value;
461 BPPY_SET_REQUIRE_VALID (self_bp);
463 if (newvalue == NULL)
465 PyErr_SetString (PyExc_TypeError,
466 _("Cannot delete `ignore_count' attribute."));
467 return -1;
469 else if (!PyLong_Check (newvalue))
471 PyErr_SetString (PyExc_TypeError,
472 _("The value of `ignore_count' must be an integer."));
473 return -1;
476 if (! gdb_py_int_as_long (newvalue, &value))
477 return -1;
479 if (value < 0)
480 value = 0;
484 set_ignore_count (self_bp->number, (int) value, 0);
486 catch (const gdb_exception &except)
488 GDB_PY_SET_HANDLE_EXCEPTION (except);
491 return 0;
494 /* Python function to set the hit count of a breakpoint. */
495 static int
496 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
498 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
500 BPPY_SET_REQUIRE_VALID (self_bp);
502 if (newvalue == NULL)
504 PyErr_SetString (PyExc_TypeError,
505 _("Cannot delete `hit_count' attribute."));
506 return -1;
508 else
510 long value;
512 if (! gdb_py_int_as_long (newvalue, &value))
513 return -1;
515 if (value != 0)
517 PyErr_SetString (PyExc_AttributeError,
518 _("The value of `hit_count' must be zero."));
519 return -1;
523 self_bp->bp->hit_count = 0;
525 return 0;
528 /* Python function to get the location of a breakpoint. */
529 static PyObject *
530 bppy_get_location (PyObject *self, void *closure)
532 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
534 BPPY_REQUIRE_VALID (obj);
536 if (obj->bp->type != bp_breakpoint
537 && obj->bp->type != bp_hardware_breakpoint)
538 Py_RETURN_NONE;
540 const char *str = obj->bp->locspec->to_string ();
541 if (str == nullptr)
542 str = "";
543 return host_string_to_python_string (str).release ();
546 /* Python function to get the breakpoint expression. */
547 static PyObject *
548 bppy_get_expression (PyObject *self, void *closure)
550 const char *str;
551 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
553 BPPY_REQUIRE_VALID (obj);
555 if (!is_watchpoint (obj->bp))
556 Py_RETURN_NONE;
558 watchpoint *wp = gdb::checked_static_cast<watchpoint *> (obj->bp);
560 str = wp->exp_string.get ();
561 if (! str)
562 str = "";
564 return host_string_to_python_string (str).release ();
567 /* Python function to get the condition expression of a breakpoint. */
568 static PyObject *
569 bppy_get_condition (PyObject *self, void *closure)
571 char *str;
572 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
574 BPPY_REQUIRE_VALID (obj);
576 str = obj->bp->cond_string.get ();
577 if (! str)
578 Py_RETURN_NONE;
580 return host_string_to_python_string (str).release ();
583 /* Returns 0 on success. Returns -1 on error, with a python exception set.
586 static int
587 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
589 gdb::unique_xmalloc_ptr<char> exp_holder;
590 const char *exp = NULL;
591 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
593 BPPY_SET_REQUIRE_VALID (self_bp);
595 if (newvalue == NULL)
597 PyErr_SetString (PyExc_TypeError,
598 _("Cannot delete `condition' attribute."));
599 return -1;
601 else if (newvalue == Py_None)
602 exp = "";
603 else
605 exp_holder = python_string_to_host_string (newvalue);
606 if (exp_holder == NULL)
607 return -1;
608 exp = exp_holder.get ();
613 set_breakpoint_condition (self_bp->bp, exp, 0, false);
615 catch (gdb_exception &ex)
617 GDB_PY_SET_HANDLE_EXCEPTION (ex);
620 return 0;
623 /* Python function to get the commands attached to a breakpoint. */
624 static PyObject *
625 bppy_get_commands (PyObject *self, void *closure)
627 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
628 struct breakpoint *bp = self_bp->bp;
630 BPPY_REQUIRE_VALID (self_bp);
632 if (! self_bp->bp->commands)
633 Py_RETURN_NONE;
635 string_file stb;
639 ui_out_redirect_pop redir (current_uiout, &stb);
640 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
642 catch (const gdb_exception &except)
644 gdbpy_convert_exception (except);
645 return NULL;
648 return host_string_to_python_string (stb.c_str ()).release ();
651 /* Set the commands attached to a breakpoint. Returns 0 on success.
652 Returns -1 on error, with a python exception set. */
653 static int
654 bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
656 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
658 BPPY_SET_REQUIRE_VALID (self_bp);
660 gdb::unique_xmalloc_ptr<char> commands
661 (python_string_to_host_string (newvalue));
662 if (commands == nullptr)
663 return -1;
667 bool first = true;
668 char *save_ptr = nullptr;
669 auto reader
670 = [&] (std::string &buffer)
672 const char *result = strtok_r (first ? commands.get () : nullptr,
673 "\n", &save_ptr);
674 first = false;
675 return result;
678 counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
679 breakpoint_set_commands (self_bp->bp, std::move (lines));
681 catch (gdb_exception &ex)
683 GDB_PY_SET_HANDLE_EXCEPTION (ex);
686 return 0;
689 /* Python function to get the breakpoint type. */
690 static PyObject *
691 bppy_get_type (PyObject *self, void *closure)
693 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
695 BPPY_REQUIRE_VALID (self_bp);
697 return gdb_py_object_from_longest (self_bp->bp->type).release ();
700 /* Python function to get the visibility of the breakpoint. */
702 static PyObject *
703 bppy_get_visibility (PyObject *self, void *closure)
705 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
707 BPPY_REQUIRE_VALID (self_bp);
709 if (user_breakpoint_p (self_bp->bp))
710 Py_RETURN_TRUE;
712 Py_RETURN_FALSE;
715 /* Python function to determine if the breakpoint is a temporary
716 breakpoint. */
718 static PyObject *
719 bppy_get_temporary (PyObject *self, void *closure)
721 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
723 BPPY_REQUIRE_VALID (self_bp);
725 if (self_bp->bp->disposition == disp_del
726 || self_bp->bp->disposition == disp_del_at_next_stop)
727 Py_RETURN_TRUE;
729 Py_RETURN_FALSE;
732 /* Python function to determine if the breakpoint is a pending
733 breakpoint. */
735 static PyObject *
736 bppy_get_pending (PyObject *self, void *closure)
738 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
740 BPPY_REQUIRE_VALID (self_bp);
742 if (is_watchpoint (self_bp->bp))
743 Py_RETURN_FALSE;
744 if (pending_breakpoint_p (self_bp->bp))
745 Py_RETURN_TRUE;
747 Py_RETURN_FALSE;
750 /* Python function to get the breakpoint's number. */
751 static PyObject *
752 bppy_get_number (PyObject *self, void *closure)
754 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
756 BPPY_REQUIRE_VALID (self_bp);
758 return gdb_py_object_from_longest (self_bp->number).release ();
761 /* Python function to get the breakpoint's thread ID. */
762 static PyObject *
763 bppy_get_thread (PyObject *self, void *closure)
765 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
767 BPPY_REQUIRE_VALID (self_bp);
769 if (self_bp->bp->thread == -1)
770 Py_RETURN_NONE;
772 return gdb_py_object_from_longest (self_bp->bp->thread).release ();
775 /* Python function to get the breakpoint's inferior ID. */
776 static PyObject *
777 bppy_get_inferior (PyObject *self, void *closure)
779 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
781 BPPY_REQUIRE_VALID (self_bp);
783 if (self_bp->bp->inferior == -1)
784 Py_RETURN_NONE;
786 return gdb_py_object_from_longest (self_bp->bp->inferior).release ();
789 /* Python function to get the breakpoint's task ID (in Ada). */
790 static PyObject *
791 bppy_get_task (PyObject *self, void *closure)
793 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
795 BPPY_REQUIRE_VALID (self_bp);
797 if (self_bp->bp->task == -1)
798 Py_RETURN_NONE;
800 return gdb_py_object_from_longest (self_bp->bp->task).release ();
803 /* Python function to get the breakpoint's hit count. */
804 static PyObject *
805 bppy_get_hit_count (PyObject *self, void *closure)
807 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
809 BPPY_REQUIRE_VALID (self_bp);
811 return gdb_py_object_from_longest (self_bp->bp->hit_count).release ();
814 /* Python function to get the breakpoint's ignore count. */
815 static PyObject *
816 bppy_get_ignore_count (PyObject *self, void *closure)
818 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
820 BPPY_REQUIRE_VALID (self_bp);
822 return gdb_py_object_from_longest (self_bp->bp->ignore_count).release ();
825 /* Python function to get the breakpoint locations of an owner breakpoint. */
827 static PyObject *
828 bppy_get_locations (PyObject *self, void *closure)
830 using py_bploc_t = gdbpy_breakpoint_location_object;
831 auto *self_bp = (gdbpy_breakpoint_object *) self;
832 BPPY_REQUIRE_VALID (self_bp);
834 gdbpy_ref<> list (PyList_New (0));
835 if (list == nullptr)
836 return nullptr;
838 for (bp_location &loc : self_bp->bp->locations ())
840 gdbpy_ref<py_bploc_t> py_bploc
841 (PyObject_New (py_bploc_t, &breakpoint_location_object_type));
842 if (py_bploc == nullptr)
843 return nullptr;
845 bp_location_ref_ptr ref = bp_location_ref_ptr::new_reference (&loc);
846 /* The location takes a reference to the owner breakpoint.
847 Decrements when they are de-allocated in bplocpy_dealloc */
848 Py_INCREF (self);
849 py_bploc->owner = self_bp;
850 py_bploc->bp_loc = ref.release ();
851 if (PyList_Append (list.get (), (PyObject *) py_bploc.get ()) != 0)
852 return nullptr;
854 return list.release ();
857 /* Internal function to validate the Python parameters/keywords
858 provided to bppy_init. */
860 static int
861 bppy_init_validate_args (const char *spec, char *source,
862 char *function, char *label,
863 char *line, enum bptype type)
865 /* If spec is defined, ensure that none of the explicit location
866 keywords are also defined. */
867 if (spec != NULL)
869 if (source != NULL || function != NULL || label != NULL || line != NULL)
871 PyErr_SetString (PyExc_RuntimeError,
872 _("Breakpoints specified with spec cannot "
873 "have source, function, label or line defined."));
874 return -1;
877 else
879 /* If spec isn't defined, ensure that the user is not trying to
880 define a watchpoint with an explicit location. */
881 if (type == bp_watchpoint)
883 PyErr_SetString (PyExc_RuntimeError,
884 _("Watchpoints cannot be set by explicit "
885 "location parameters."));
886 return -1;
888 else
890 /* Otherwise, ensure some explicit locations are defined. */
891 if (source == NULL && function == NULL && label == NULL
892 && line == NULL)
894 PyErr_SetString (PyExc_RuntimeError,
895 _("Neither spec nor explicit location set."));
896 return -1;
898 /* Finally, if source is specified, ensure that line, label
899 or function are specified too. */
900 if (source != NULL && function == NULL && label == NULL
901 && line == NULL)
903 PyErr_SetString (PyExc_RuntimeError,
904 _("Specifying a source must also include a "
905 "line, label or function."));
906 return -1;
910 return 1;
913 /* Python function to create a new breakpoint. */
914 static int
915 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
917 static const char *keywords[] = { "spec", "type", "wp_class", "internal",
918 "temporary","source", "function",
919 "label", "line", "qualified", NULL };
920 const char *spec = NULL;
921 enum bptype type = bp_breakpoint;
922 int access_type = hw_write;
923 PyObject *internal = NULL;
924 PyObject *temporary = NULL;
925 PyObject *lineobj = NULL;;
926 int internal_bp = 0;
927 int temporary_bp = 0;
928 gdb::unique_xmalloc_ptr<char> line;
929 char *label = NULL;
930 char *source = NULL;
931 char *function = NULL;
932 PyObject * qualified = NULL;
934 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
935 &spec, &type, &access_type,
936 &internal,
937 &temporary, &source,
938 &function, &label, &lineobj,
939 &qualified))
940 return -1;
943 if (lineobj != NULL)
945 if (PyLong_Check (lineobj))
946 line = xstrprintf ("%ld", PyLong_AsLong (lineobj));
947 else if (PyUnicode_Check (lineobj))
948 line = python_string_to_host_string (lineobj);
949 else
951 PyErr_SetString (PyExc_RuntimeError,
952 _("Line keyword should be an integer or a string. "));
953 return -1;
957 if (internal)
959 internal_bp = PyObject_IsTrue (internal);
960 if (internal_bp == -1)
961 return -1;
964 if (temporary != NULL)
966 temporary_bp = PyObject_IsTrue (temporary);
967 if (temporary_bp == -1)
968 return -1;
971 if (bppy_init_validate_args (spec, source, function, label, line.get (),
972 type) == -1)
973 return -1;
975 bppy_pending_object = (gdbpy_breakpoint_object *) self;
976 bppy_pending_object->number = -1;
977 bppy_pending_object->bp = NULL;
981 switch (type)
983 case bp_breakpoint:
984 case bp_hardware_breakpoint:
986 location_spec_up locspec;
987 symbol_name_match_type func_name_match_type
988 = (qualified != NULL && PyObject_IsTrue (qualified)
989 ? symbol_name_match_type::FULL
990 : symbol_name_match_type::WILD);
992 if (spec != NULL)
994 gdb::unique_xmalloc_ptr<char>
995 copy_holder (xstrdup (skip_spaces (spec)));
996 const char *copy = copy_holder.get ();
998 locspec = string_to_location_spec (&copy,
999 current_language,
1000 func_name_match_type);
1002 else
1004 std::unique_ptr<explicit_location_spec> explicit_loc
1005 (new explicit_location_spec ());
1007 if (source != nullptr)
1008 explicit_loc->source_filename = make_unique_xstrdup (source);
1009 if (function != nullptr)
1010 explicit_loc->function_name = make_unique_xstrdup (function);
1011 if (label != nullptr)
1012 explicit_loc->label_name = make_unique_xstrdup (label);
1014 if (line != NULL)
1015 explicit_loc->line_offset
1016 = linespec_parse_line_offset (line.get ());
1018 explicit_loc->func_name_match_type = func_name_match_type;
1020 locspec.reset (explicit_loc.release ());
1023 const struct breakpoint_ops *ops
1024 = breakpoint_ops_for_location_spec (locspec.get (), false);
1026 create_breakpoint (gdbpy_enter::get_gdbarch (),
1027 locspec.get (), NULL, -1, -1, NULL, false,
1029 temporary_bp, type,
1031 AUTO_BOOLEAN_TRUE,
1032 ops,
1033 0, 1, internal_bp, 0);
1034 break;
1036 case bp_watchpoint:
1038 spec = skip_spaces (spec);
1040 if (access_type == hw_write)
1041 watch_command_wrapper (spec, 0, internal_bp);
1042 else if (access_type == hw_access)
1043 awatch_command_wrapper (spec, 0, internal_bp);
1044 else if (access_type == hw_read)
1045 rwatch_command_wrapper (spec, 0, internal_bp);
1046 else
1047 error(_("Cannot understand watchpoint access type."));
1048 break;
1050 case bp_catchpoint:
1051 error (_("BP_CATCHPOINT not supported"));
1052 default:
1053 error(_("Do not understand breakpoint type to set."));
1056 catch (const gdb_exception &except)
1058 bppy_pending_object = NULL;
1059 gdbpy_convert_exception (except);
1060 return -1;
1063 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
1064 return 0;
1067 /* __repr__ implementation for gdb.Breakpoint. */
1069 static PyObject *
1070 bppy_repr (PyObject *self)
1072 const auto bp = (struct gdbpy_breakpoint_object*) self;
1073 if (bp->bp == nullptr)
1074 return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
1076 std::string str = " ";
1077 if (bp->bp->thread != -1)
1078 str += string_printf ("thread=%d ", bp->bp->thread);
1079 if (bp->bp->task > 0)
1080 str += string_printf ("task=%d ", bp->bp->task);
1081 if (bp->bp->enable_count > 0)
1082 str += string_printf ("enable_count=%d ", bp->bp->enable_count);
1083 str.pop_back ();
1085 return PyUnicode_FromFormat ("<%s%s number=%d hits=%d%s>",
1086 Py_TYPE (self)->tp_name,
1087 (bp->bp->enable_state == bp_enabled
1088 ? "" : " disabled"), bp->bp->number,
1089 bp->bp->hit_count, str.c_str ());
1092 /* Append to LIST the breakpoint Python object associated to B.
1094 Return true on success. Return false on failure, with the Python error
1095 indicator set. */
1097 static bool
1098 build_bp_list (struct breakpoint *b, PyObject *list)
1100 PyObject *bp = (PyObject *) b->py_bp_object;
1102 /* Not all breakpoints will have a companion Python object.
1103 Only breakpoints that were created via bppy_new, or
1104 breakpoints that were created externally and are tracked by
1105 the Python Scripting API. */
1106 if (bp == nullptr)
1107 return true;
1109 return PyList_Append (list, bp) == 0;
1112 /* See python-internal.h. */
1114 bool
1115 gdbpy_breakpoint_init_breakpoint_type ()
1117 if (breakpoint_object_type.tp_new == nullptr)
1119 breakpoint_object_type.tp_new = PyType_GenericNew;
1120 if (PyType_Ready (&breakpoint_object_type) < 0)
1122 /* Reset tp_new back to nullptr so future calls to this function
1123 will try calling PyType_Ready again. */
1124 breakpoint_object_type.tp_new = nullptr;
1125 return false;
1129 return true;
1132 /* Static function to return a tuple holding all breakpoints. */
1134 PyObject *
1135 gdbpy_breakpoints (PyObject *self, PyObject *args)
1137 if (bppy_live == 0)
1138 return PyTuple_New (0);
1140 gdbpy_ref<> list (PyList_New (0));
1141 if (list == NULL)
1142 return NULL;
1144 /* If build_bp_list returns false, it signals an error condition. In that
1145 case abandon building the list and return nullptr. */
1146 for (breakpoint &bp : all_breakpoints ())
1147 if (!build_bp_list (&bp, list.get ()))
1148 return nullptr;
1150 return PyList_AsTuple (list.get ());
1153 /* Call the "stop" method (if implemented) in the breakpoint
1154 class. If the method returns True, the inferior will be
1155 stopped at the breakpoint. Otherwise the inferior will be
1156 allowed to continue. */
1158 enum ext_lang_bp_stop
1159 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
1160 struct breakpoint *b)
1162 int stop;
1163 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
1164 PyObject *py_bp = (PyObject *) bp_obj;
1166 if (bp_obj == NULL)
1167 return EXT_LANG_BP_STOP_UNSET;
1169 stop = -1;
1171 gdbpy_enter enter_py (b->gdbarch);
1173 if (bp_obj->is_finish_bp)
1174 bpfinishpy_pre_stop_hook (bp_obj);
1176 if (PyObject_HasAttrString (py_bp, stop_func))
1178 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
1180 stop = 1;
1181 if (result != NULL)
1183 int evaluate = PyObject_IsTrue (result.get ());
1185 if (evaluate == -1)
1186 gdbpy_print_stack ();
1188 /* If the "stop" function returns False that means
1189 the Python breakpoint wants GDB to continue. */
1190 if (! evaluate)
1191 stop = 0;
1193 else
1194 gdbpy_print_stack ();
1197 if (bp_obj->is_finish_bp)
1198 bpfinishpy_post_stop_hook (bp_obj);
1200 if (stop < 0)
1201 return EXT_LANG_BP_STOP_UNSET;
1202 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
1205 /* Checks if the "stop" method exists in this breakpoint.
1206 Used by condition_command to ensure mutual exclusion of breakpoint
1207 conditions. */
1210 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
1211 struct breakpoint *b)
1213 PyObject *py_bp;
1215 if (b->py_bp_object == NULL)
1216 return 0;
1218 py_bp = (PyObject *) b->py_bp_object;
1220 gdbpy_enter enter_py (b->gdbarch);
1221 return PyObject_HasAttrString (py_bp, stop_func);
1226 /* Event callback functions. */
1228 /* Callback that is used when a breakpoint is created. This function
1229 will create a new Python breakpoint object. */
1230 static void
1231 gdbpy_breakpoint_created (struct breakpoint *bp)
1233 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1235 gdbpy_breakpoint_object *newbp;
1237 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
1239 pybp_debug_printf ("not attaching python object to this breakpoint");
1240 return;
1243 if (bp->type != bp_breakpoint
1244 && bp->type != bp_hardware_breakpoint
1245 && bp->type != bp_watchpoint
1246 && bp->type != bp_hardware_watchpoint
1247 && bp->type != bp_read_watchpoint
1248 && bp->type != bp_access_watchpoint
1249 && bp->type != bp_catchpoint)
1251 pybp_debug_printf ("is not a breakpoint or watchpoint");
1252 return;
1255 gdbpy_enter enter_py (bp->gdbarch);
1257 if (bppy_pending_object)
1259 newbp = bppy_pending_object;
1260 Py_INCREF (newbp);
1261 bppy_pending_object = NULL;
1262 pybp_debug_printf ("attaching existing breakpoint object");
1264 else
1266 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
1267 pybp_debug_printf ("attaching new breakpoint object");
1269 if (newbp)
1271 newbp->number = bp->number;
1272 newbp->bp = bp;
1273 newbp->bp->py_bp_object = newbp;
1274 newbp->is_finish_bp = 0;
1275 ++bppy_live;
1277 else
1279 PyErr_SetString (PyExc_RuntimeError,
1280 _("Error while creating breakpoint from GDB."));
1281 gdbpy_print_stack ();
1284 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1286 if (evpy_emit_event ((PyObject *) newbp,
1287 gdb_py_events.breakpoint_created) < 0)
1288 gdbpy_print_stack ();
1292 /* Callback that is used when a breakpoint is deleted. This will
1293 invalidate the corresponding Python object. */
1294 static void
1295 gdbpy_breakpoint_deleted (struct breakpoint *b)
1297 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1299 int num = b->number;
1300 struct breakpoint *bp = NULL;
1302 bp = get_breakpoint (num);
1303 if (bp)
1305 gdbpy_enter enter_py (b->gdbarch);
1307 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1308 if (bp_obj != NULL)
1310 if (bp_obj->is_finish_bp)
1311 bpfinishpy_pre_delete_hook (bp_obj.get ());
1313 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1315 if (evpy_emit_event ((PyObject *) bp_obj.get (),
1316 gdb_py_events.breakpoint_deleted) < 0)
1317 gdbpy_print_stack ();
1320 bp_obj->bp = NULL;
1321 --bppy_live;
1326 /* Callback that is used when a breakpoint is modified. */
1328 static void
1329 gdbpy_breakpoint_modified (struct breakpoint *b)
1331 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1333 int num = b->number;
1334 struct breakpoint *bp = NULL;
1336 bp = get_breakpoint (num);
1337 if (bp)
1339 gdbpy_enter enter_py (b->gdbarch);
1341 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1342 if (bp_obj)
1344 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1346 if (evpy_emit_event (bp_obj,
1347 gdb_py_events.breakpoint_modified) < 0)
1348 gdbpy_print_stack ();
1356 /* Initialize the Python breakpoint code. */
1357 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
1358 gdbpy_initialize_breakpoints (void)
1360 int i;
1362 if (!gdbpy_breakpoint_init_breakpoint_type ())
1363 return -1;
1365 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1366 (PyObject *) &breakpoint_object_type) < 0)
1367 return -1;
1369 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created,
1370 "py-breakpoint");
1371 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted,
1372 "py-breakpoint");
1373 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified,
1374 "py-breakpoint");
1376 /* Add breakpoint types constants. */
1377 for (i = 0; pybp_codes[i].name; ++i)
1379 if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
1380 pybp_codes[i].code) < 0)
1381 return -1;
1384 /* Add watchpoint types constants. */
1385 for (i = 0; pybp_watch_types[i].name; ++i)
1387 if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
1388 pybp_watch_types[i].code) < 0)
1389 return -1;
1392 return 0;
1395 /* Initialize the Python BreakpointLocation code. */
1397 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
1398 gdbpy_initialize_breakpoint_locations ()
1400 if (PyType_Ready (&breakpoint_location_object_type) < 0)
1401 return -1;
1403 if (gdb_pymodule_addobject (gdb_module, "BreakpointLocation",
1404 (PyObject *) &breakpoint_location_object_type)
1405 < 0)
1406 return -1;
1407 return 0;
1412 /* Helper function that overrides this Python object's
1413 PyObject_GenericSetAttr to allow extra validation of the attribute
1414 being set. */
1416 static int
1417 local_setattro (PyObject *self, PyObject *name, PyObject *v)
1419 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1420 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
1422 if (attr == NULL)
1423 return -1;
1425 /* If the attribute trying to be set is the "stop" method,
1426 but we already have a condition set in the CLI or other extension
1427 language, disallow this operation. */
1428 if (strcmp (attr.get (), stop_func) == 0)
1430 const struct extension_language_defn *extlang = NULL;
1432 if (obj->bp->cond_string != NULL)
1433 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1434 if (extlang == NULL)
1435 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1436 if (extlang != NULL)
1438 std::string error_text
1439 = string_printf (_("Only one stop condition allowed. There is"
1440 " currently a %s stop condition defined for"
1441 " this breakpoint."),
1442 ext_lang_capitalized_name (extlang));
1443 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
1444 return -1;
1448 return PyObject_GenericSetAttr (self, name, v);
1451 static gdb_PyGetSetDef breakpoint_object_getset[] = {
1452 { "enabled", bppy_get_enabled, bppy_set_enabled,
1453 "Boolean telling whether the breakpoint is enabled.", NULL },
1454 { "silent", bppy_get_silent, bppy_set_silent,
1455 "Boolean telling whether the breakpoint is silent.", NULL },
1456 { "thread", bppy_get_thread, bppy_set_thread,
1457 "Thread ID for the breakpoint.\n\
1458 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1459 If the value is None, then this breakpoint is not thread-specific.\n\
1460 No other type of value can be used.", NULL },
1461 { "inferior", bppy_get_inferior, bppy_set_inferior,
1462 "Inferior ID for the breakpoint.\n\
1463 If the value is an inferior ID (integer), then this is an inferior-specific\n\
1464 breakpoint. If the value is None, then this breakpoint is not\n\
1465 inferior-specific. No other type of value can be used.", NULL },
1466 { "task", bppy_get_task, bppy_set_task,
1467 "Thread ID for the breakpoint.\n\
1468 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1469 If the value is None, then this breakpoint is not task-specific.\n\
1470 No other type of value can be used.", NULL },
1471 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1472 "Number of times this breakpoint should be automatically continued.",
1473 NULL },
1474 { "number", bppy_get_number, NULL,
1475 "Breakpoint's number assigned by GDB.", NULL },
1476 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1477 "Number of times the breakpoint has been hit.\n\
1478 Can be set to zero to clear the count. No other value is valid\n\
1479 when setting this property.", NULL },
1480 { "location", bppy_get_location, NULL,
1481 "Location of the breakpoint, as specified by the user.", NULL},
1482 { "expression", bppy_get_expression, NULL,
1483 "Expression of the breakpoint, as specified by the user.", NULL},
1484 { "condition", bppy_get_condition, bppy_set_condition,
1485 "Condition of the breakpoint, as specified by the user,\
1486 or None if no condition set."},
1487 { "commands", bppy_get_commands, bppy_set_commands,
1488 "Commands of the breakpoint, as specified by the user."},
1489 { "type", bppy_get_type, NULL,
1490 "Type of breakpoint."},
1491 { "visible", bppy_get_visibility, NULL,
1492 "Whether the breakpoint is visible to the user."},
1493 { "temporary", bppy_get_temporary, NULL,
1494 "Whether this breakpoint is a temporary breakpoint."},
1495 { "pending", bppy_get_pending, NULL,
1496 "Whether this breakpoint is a pending breakpoint."},
1497 { "locations", bppy_get_locations, NULL,
1498 "Get locations where this breakpoint was set"},
1499 { NULL } /* Sentinel. */
1502 static PyMethodDef breakpoint_object_methods[] =
1504 { "is_valid", bppy_is_valid, METH_NOARGS,
1505 "Return true if this breakpoint is valid, false if not." },
1506 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1507 "Delete the underlying GDB breakpoint." },
1508 { NULL } /* Sentinel. */
1511 PyTypeObject breakpoint_object_type =
1513 PyVarObject_HEAD_INIT (NULL, 0)
1514 "gdb.Breakpoint", /*tp_name*/
1515 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1516 0, /*tp_itemsize*/
1517 0, /*tp_dealloc*/
1518 0, /*tp_print*/
1519 0, /*tp_getattr*/
1520 0, /*tp_setattr*/
1521 0, /*tp_compare*/
1522 bppy_repr, /*tp_repr*/
1523 0, /*tp_as_number*/
1524 0, /*tp_as_sequence*/
1525 0, /*tp_as_mapping*/
1526 0, /*tp_hash */
1527 0, /*tp_call*/
1528 0, /*tp_str*/
1529 0, /*tp_getattro*/
1530 (setattrofunc)local_setattro, /*tp_setattro */
1531 0, /*tp_as_buffer*/
1532 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1533 "GDB breakpoint object", /* tp_doc */
1534 0, /* tp_traverse */
1535 0, /* tp_clear */
1536 0, /* tp_richcompare */
1537 0, /* tp_weaklistoffset */
1538 0, /* tp_iter */
1539 0, /* tp_iternext */
1540 breakpoint_object_methods, /* tp_methods */
1541 0, /* tp_members */
1542 breakpoint_object_getset, /* tp_getset */
1543 0, /* tp_base */
1544 0, /* tp_dict */
1545 0, /* tp_descr_get */
1546 0, /* tp_descr_set */
1547 0, /* tp_dictoffset */
1548 bppy_init, /* tp_init */
1549 0, /* tp_alloc */
1552 void _initialize_py_breakpoint ();
1553 void
1554 _initialize_py_breakpoint ()
1556 add_setshow_boolean_cmd
1557 ("py-breakpoint", class_maintenance, &pybp_debug,
1558 _("Set Python breakpoint debugging."),
1559 _("Show Python breakpoint debugging."),
1560 _("When on, Python breakpoint debugging is enabled."),
1561 NULL,
1562 show_pybp_debug,
1563 &setdebuglist, &showdebuglist);
1566 GDBPY_INITIALIZE_FILE (gdbpy_initialize_breakpoints);
1567 GDBPY_INITIALIZE_FILE (gdbpy_initialize_breakpoint_locations);
1569 /* Python function to set the enabled state of a breakpoint location. */
1571 static int
1572 bplocpy_set_enabled (PyObject *py_self, PyObject *newvalue, void *closure)
1574 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1575 BPPY_SET_REQUIRE_VALID (self->owner);
1576 BPLOCPY_SET_REQUIRE_VALID (self->owner, self);
1578 if (newvalue == nullptr)
1580 PyErr_SetString (PyExc_TypeError,
1581 _("Cannot delete 'enabled' attribute."));
1582 return -1;
1584 else if (!PyBool_Check (newvalue))
1586 PyErr_SetString (PyExc_TypeError,
1587 _("The value of 'enabled' must be a boolean."));
1588 return -1;
1591 int cmp = PyObject_IsTrue (newvalue);
1592 if (cmp < 0)
1593 return -1;
1597 enable_disable_bp_location (self->bp_loc, cmp == 1);
1599 catch (const gdb_exception &except)
1601 GDB_PY_SET_HANDLE_EXCEPTION (except);
1603 return 0;
1606 /* Python function to test whether or not the breakpoint location is enabled. */
1608 static PyObject *
1609 bplocpy_get_enabled (PyObject *py_self, void *closure)
1611 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1612 BPPY_REQUIRE_VALID (self->owner);
1613 BPLOCPY_REQUIRE_VALID (self->owner, self);
1615 if (self->bp_loc->enabled)
1616 Py_RETURN_TRUE;
1617 else
1618 Py_RETURN_FALSE;
1621 /* Python function to get address of breakpoint location. */
1623 static PyObject *
1624 bplocpy_get_address (PyObject *py_self, void *closure)
1626 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1627 BPPY_REQUIRE_VALID (self->owner);
1628 BPLOCPY_REQUIRE_VALID (self->owner, self);
1629 return gdb_py_object_from_ulongest (self->bp_loc->address).release ();
1632 /* Python function to get owner of breakpoint location, which
1633 is of type gdb.Breakpoint. */
1635 static PyObject *
1636 bplocpy_get_owner (PyObject *py_self, void *closure)
1638 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1639 BPPY_REQUIRE_VALID (self->owner);
1640 BPLOCPY_REQUIRE_VALID (self->owner, self);
1641 Py_INCREF (self->owner);
1642 return (PyObject *) self->owner;
1645 /* Python function to get the source file name path and line number
1646 where this breakpoint location was set. */
1648 static PyObject *
1649 bplocpy_get_source_location (PyObject *py_self, void *closure)
1651 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1652 BPPY_REQUIRE_VALID (self->owner);
1653 BPLOCPY_REQUIRE_VALID (self->owner, self);
1654 if (self->bp_loc->symtab)
1656 gdbpy_ref<> tup (PyTuple_New (2));
1657 if (tup == nullptr)
1658 return nullptr;
1659 /* symtab->filename is never NULL. */
1660 gdbpy_ref<> filename
1661 = host_string_to_python_string (self->bp_loc->symtab->filename);
1662 if (filename == nullptr)
1663 return nullptr;
1664 auto line = gdb_py_object_from_ulongest (self->bp_loc->line_number);
1665 if (line == nullptr)
1666 return nullptr;
1667 if (PyTuple_SetItem (tup.get (), 0, filename.release ()) == -1
1668 || PyTuple_SetItem (tup.get (), 1, line.release ()) == -1)
1669 return nullptr;
1670 return tup.release ();
1672 else
1673 Py_RETURN_NONE;
1676 /* Python function to get the function name of where this location was set. */
1678 static PyObject *
1679 bplocpy_get_function (PyObject *py_self, void *closure)
1681 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1682 BPPY_REQUIRE_VALID (self->owner);
1683 BPLOCPY_REQUIRE_VALID (self->owner, self);
1684 const auto fn_name = self->bp_loc->function_name.get ();
1685 if (fn_name != nullptr)
1686 return host_string_to_python_string (fn_name).release ();
1687 Py_RETURN_NONE;
1690 static PyObject *
1691 bplocpy_get_thread_groups (PyObject *py_self, void *closure)
1693 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1694 BPPY_REQUIRE_VALID (self->owner);
1695 BPLOCPY_REQUIRE_VALID (self->owner, self);
1696 gdbpy_ref<> list (PyList_New (0));
1697 if (list == nullptr)
1698 return nullptr;
1699 for (inferior *inf : all_inferiors ())
1701 if (inf->pspace == self->bp_loc->pspace)
1703 gdbpy_ref<> num = gdb_py_object_from_ulongest (inf->num);
1704 if (num == nullptr)
1705 return nullptr;
1706 if (PyList_Append (list.get (), num.release ()) != 0)
1707 return nullptr;
1710 return list.release ();
1713 static PyObject *
1714 bplocpy_get_fullname (PyObject *py_self, void *closure)
1716 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1717 BPPY_REQUIRE_VALID (self->owner);
1718 BPLOCPY_REQUIRE_VALID (self->owner, self);
1719 const auto symtab = self->bp_loc->symtab;
1720 if (symtab != nullptr && symtab->fullname != nullptr)
1722 gdbpy_ref<> fullname
1723 = host_string_to_python_string (symtab->fullname);
1724 return fullname.release ();
1726 Py_RETURN_NONE;
1729 /* De-allocation function to be called for the Python object. */
1731 static void
1732 bplocpy_dealloc (PyObject *py_self)
1734 auto *self = (gdbpy_breakpoint_location_object *) py_self;
1735 bp_location_ref_ptr decrementing_ref {self->bp_loc};
1736 Py_XDECREF (self->owner);
1737 Py_TYPE (py_self)->tp_free (py_self);
1740 /* __repr__ implementation for gdb.BreakpointLocation. */
1742 static PyObject *
1743 bplocpy_repr (PyObject *py_self)
1745 const auto self = (gdbpy_breakpoint_location_object *) py_self;
1746 if (self->owner == nullptr || self->owner->bp == nullptr
1747 || self->owner->bp != self->bp_loc->owner)
1748 return gdb_py_invalid_object_repr (py_self);
1750 const auto enabled = self->bp_loc->enabled ? "enabled" : "disabled";
1752 std::string str (enabled);
1754 str += string_printf (" address=%s",
1755 paddress (self->bp_loc->owner->gdbarch,
1756 self->bp_loc->address));
1758 if (self->bp_loc->requested_address != self->bp_loc->address)
1759 str += string_printf (" requested_address=%s",
1760 paddress (self->bp_loc->owner->gdbarch,
1761 self->bp_loc->requested_address));
1762 if (self->bp_loc->symtab != nullptr)
1763 str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename,
1764 self->bp_loc->line_number);
1766 const auto fn_name = self->bp_loc->function_name.get ();
1767 if (fn_name != nullptr)
1769 str += " in ";
1770 str += fn_name;
1773 return PyUnicode_FromFormat ("<%s %s>", Py_TYPE (self)->tp_name,
1774 str.c_str ());
1777 /* Attribute get/set Python definitions. */
1779 static gdb_PyGetSetDef bp_location_object_getset[] = {
1780 { "enabled", bplocpy_get_enabled, bplocpy_set_enabled,
1781 "Boolean telling whether the breakpoint is enabled.", NULL },
1782 { "owner", bplocpy_get_owner, NULL,
1783 "Get the breakpoint owner object", NULL },
1784 { "address", bplocpy_get_address, NULL,
1785 "Get address of where this location was set", NULL},
1786 { "source", bplocpy_get_source_location, NULL,
1787 "Get file and line number of where this location was set", NULL},
1788 { "function", bplocpy_get_function, NULL,
1789 "Get function of where this location was set", NULL },
1790 { "fullname", bplocpy_get_fullname, NULL,
1791 "Get fullname of where this location was set", NULL },
1792 { "thread_groups", bplocpy_get_thread_groups, NULL,
1793 "Get thread groups where this location is in", NULL },
1794 { NULL } /* Sentinel. */
1797 PyTypeObject breakpoint_location_object_type =
1799 PyVarObject_HEAD_INIT (NULL, 0)
1800 "gdb.BreakpointLocation", /*tp_name*/
1801 sizeof (gdbpy_breakpoint_location_object), /*tp_basicsize*/
1802 0, /*tp_itemsize*/
1803 bplocpy_dealloc, /*tp_dealloc*/
1804 0, /*tp_print*/
1805 0, /*tp_getattr*/
1806 0, /*tp_setattr*/
1807 0, /*tp_compare*/
1808 bplocpy_repr, /*tp_repr*/
1809 0, /*tp_as_number*/
1810 0, /*tp_as_sequence*/
1811 0, /*tp_as_mapping*/
1812 0, /*tp_hash */
1813 0, /*tp_call*/
1814 0, /*tp_str*/
1815 0, /*tp_getattro*/
1816 0, /*tp_setattro */
1817 0, /*tp_as_buffer*/
1818 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1819 "GDB breakpoint location object", /* tp_doc */
1820 0, /* tp_traverse */
1821 0, /* tp_clear */
1822 0, /* tp_richcompare */
1823 0, /* tp_weaklistoffset */
1824 0, /* tp_iter */
1825 0, /* tp_iternext */
1826 0, /* tp_methods */
1827 0, /* tp_members */
1828 bp_location_object_getset, /* tp_getset */
1829 0, /* tp_base */
1830 0, /* tp_dict */
1831 0, /* tp_descr_get */
1832 0, /* tp_descr_set */
1833 0, /* tp_dictoffset */
1834 0, /* tp_init */
1835 0, /* tp_alloc */