Update copyright year range in all GDB files.
[binutils-gdb.git] / gdb / python / py-breakpoint.c
blob11345ad305267b9b7df439492f1a69a19cfab669
1 /* Python interface to breakpoints
3 Copyright (C) 2008-2020 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"
37 /* Number of live breakpoints. */
38 static int bppy_live;
40 /* Variables used to pass information between the Breakpoint
41 constructor and the breakpoint-created hook function. */
42 gdbpy_breakpoint_object *bppy_pending_object;
44 /* Function that is called when a Python condition is evaluated. */
45 static const char stop_func[] = "stop";
47 /* This is used to initialize various gdb.bp_* constants. */
48 struct pybp_code
50 /* The name. */
51 const char *name;
52 /* The code. */
53 int code;
56 /* Entries related to the type of user set breakpoints. */
57 static struct pybp_code pybp_codes[] =
59 { "BP_NONE", bp_none},
60 { "BP_BREAKPOINT", bp_breakpoint},
61 { "BP_WATCHPOINT", bp_watchpoint},
62 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
63 { "BP_READ_WATCHPOINT", bp_read_watchpoint},
64 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
65 {NULL} /* Sentinel. */
68 /* Entries related to the type of watchpoint. */
69 static struct pybp_code pybp_watch_types[] =
71 { "WP_READ", hw_read},
72 { "WP_WRITE", hw_write},
73 { "WP_ACCESS", hw_access},
74 {NULL} /* Sentinel. */
77 /* Python function which checks the validity of a breakpoint object. */
78 static PyObject *
79 bppy_is_valid (PyObject *self, PyObject *args)
81 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
83 if (self_bp->bp)
84 Py_RETURN_TRUE;
85 Py_RETURN_FALSE;
88 /* Python function to test whether or not the breakpoint is enabled. */
89 static PyObject *
90 bppy_get_enabled (PyObject *self, void *closure)
92 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
94 BPPY_REQUIRE_VALID (self_bp);
95 if (! self_bp->bp)
96 Py_RETURN_FALSE;
97 if (self_bp->bp->enable_state == bp_enabled)
98 Py_RETURN_TRUE;
99 Py_RETURN_FALSE;
102 /* Python function to test whether or not the breakpoint is silent. */
103 static PyObject *
104 bppy_get_silent (PyObject *self, void *closure)
106 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
108 BPPY_REQUIRE_VALID (self_bp);
109 if (self_bp->bp->silent)
110 Py_RETURN_TRUE;
111 Py_RETURN_FALSE;
114 /* Python function to set the enabled state of a breakpoint. */
115 static int
116 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
118 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
119 int cmp;
121 BPPY_SET_REQUIRE_VALID (self_bp);
123 if (newvalue == NULL)
125 PyErr_SetString (PyExc_TypeError,
126 _("Cannot delete `enabled' attribute."));
128 return -1;
130 else if (! PyBool_Check (newvalue))
132 PyErr_SetString (PyExc_TypeError,
133 _("The value of `enabled' must be a boolean."));
134 return -1;
137 cmp = PyObject_IsTrue (newvalue);
138 if (cmp < 0)
139 return -1;
143 if (cmp == 1)
144 enable_breakpoint (self_bp->bp);
145 else
146 disable_breakpoint (self_bp->bp);
148 catch (const gdb_exception &except)
150 GDB_PY_SET_HANDLE_EXCEPTION (except);
153 return 0;
156 /* Python function to set the 'silent' state of a breakpoint. */
157 static int
158 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
160 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
161 int cmp;
163 BPPY_SET_REQUIRE_VALID (self_bp);
165 if (newvalue == NULL)
167 PyErr_SetString (PyExc_TypeError,
168 _("Cannot delete `silent' attribute."));
169 return -1;
171 else if (! PyBool_Check (newvalue))
173 PyErr_SetString (PyExc_TypeError,
174 _("The value of `silent' must be a boolean."));
175 return -1;
178 cmp = PyObject_IsTrue (newvalue);
179 if (cmp < 0)
180 return -1;
181 else
182 breakpoint_set_silent (self_bp->bp, cmp);
184 return 0;
187 /* Python function to set the thread of a breakpoint. */
188 static int
189 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
191 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
192 long id;
194 BPPY_SET_REQUIRE_VALID (self_bp);
196 if (newvalue == NULL)
198 PyErr_SetString (PyExc_TypeError,
199 _("Cannot delete `thread' attribute."));
200 return -1;
202 else if (PyInt_Check (newvalue))
204 if (! gdb_py_int_as_long (newvalue, &id))
205 return -1;
207 if (!valid_global_thread_id (id))
209 PyErr_SetString (PyExc_RuntimeError,
210 _("Invalid thread ID."));
211 return -1;
214 else if (newvalue == Py_None)
215 id = -1;
216 else
218 PyErr_SetString (PyExc_TypeError,
219 _("The value of `thread' must be an integer or None."));
220 return -1;
223 breakpoint_set_thread (self_bp->bp, id);
225 return 0;
228 /* Python function to set the (Ada) task of a breakpoint. */
229 static int
230 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
232 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
233 long id;
234 int valid_id = 0;
236 BPPY_SET_REQUIRE_VALID (self_bp);
238 if (newvalue == NULL)
240 PyErr_SetString (PyExc_TypeError,
241 _("Cannot delete `task' attribute."));
242 return -1;
244 else if (PyInt_Check (newvalue))
246 if (! gdb_py_int_as_long (newvalue, &id))
247 return -1;
251 valid_id = valid_task_id (id);
253 catch (const gdb_exception &except)
255 GDB_PY_SET_HANDLE_EXCEPTION (except);
258 if (! valid_id)
260 PyErr_SetString (PyExc_RuntimeError,
261 _("Invalid task ID."));
262 return -1;
265 else if (newvalue == Py_None)
266 id = 0;
267 else
269 PyErr_SetString (PyExc_TypeError,
270 _("The value of `task' must be an integer or None."));
271 return -1;
274 breakpoint_set_task (self_bp->bp, id);
276 return 0;
279 /* Python function which deletes the underlying GDB breakpoint. This
280 triggers the breakpoint_deleted observer which will call
281 gdbpy_breakpoint_deleted; that function cleans up the Python
282 sections. */
284 static PyObject *
285 bppy_delete_breakpoint (PyObject *self, PyObject *args)
287 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
289 BPPY_REQUIRE_VALID (self_bp);
293 delete_breakpoint (self_bp->bp);
295 catch (const gdb_exception &except)
297 GDB_PY_HANDLE_EXCEPTION (except);
300 Py_RETURN_NONE;
304 /* Python function to set the ignore count of a breakpoint. */
305 static int
306 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
308 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
309 long value;
311 BPPY_SET_REQUIRE_VALID (self_bp);
313 if (newvalue == NULL)
315 PyErr_SetString (PyExc_TypeError,
316 _("Cannot delete `ignore_count' attribute."));
317 return -1;
319 else if (! PyInt_Check (newvalue))
321 PyErr_SetString (PyExc_TypeError,
322 _("The value of `ignore_count' must be an integer."));
323 return -1;
326 if (! gdb_py_int_as_long (newvalue, &value))
327 return -1;
329 if (value < 0)
330 value = 0;
334 set_ignore_count (self_bp->number, (int) value, 0);
336 catch (const gdb_exception &except)
338 GDB_PY_SET_HANDLE_EXCEPTION (except);
341 return 0;
344 /* Python function to set the hit count of a breakpoint. */
345 static int
346 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
348 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
350 BPPY_SET_REQUIRE_VALID (self_bp);
352 if (newvalue == NULL)
354 PyErr_SetString (PyExc_TypeError,
355 _("Cannot delete `hit_count' attribute."));
356 return -1;
358 else
360 long value;
362 if (! gdb_py_int_as_long (newvalue, &value))
363 return -1;
365 if (value != 0)
367 PyErr_SetString (PyExc_AttributeError,
368 _("The value of `hit_count' must be zero."));
369 return -1;
373 self_bp->bp->hit_count = 0;
375 return 0;
378 /* Python function to get the location of a breakpoint. */
379 static PyObject *
380 bppy_get_location (PyObject *self, void *closure)
382 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
384 BPPY_REQUIRE_VALID (obj);
386 if (obj->bp->type != bp_breakpoint)
387 Py_RETURN_NONE;
389 const char *str = event_location_to_string (obj->bp->location.get ());
390 if (! str)
391 str = "";
392 return host_string_to_python_string (str).release ();
395 /* Python function to get the breakpoint expression. */
396 static PyObject *
397 bppy_get_expression (PyObject *self, void *closure)
399 const char *str;
400 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
401 struct watchpoint *wp;
403 BPPY_REQUIRE_VALID (obj);
405 if (!is_watchpoint (obj->bp))
406 Py_RETURN_NONE;
408 wp = (struct watchpoint *) obj->bp;
410 str = wp->exp_string;
411 if (! str)
412 str = "";
414 return host_string_to_python_string (str).release ();
417 /* Python function to get the condition expression of a breakpoint. */
418 static PyObject *
419 bppy_get_condition (PyObject *self, void *closure)
421 char *str;
422 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
424 BPPY_REQUIRE_VALID (obj);
426 str = obj->bp->cond_string;
427 if (! str)
428 Py_RETURN_NONE;
430 return host_string_to_python_string (str).release ();
433 /* Returns 0 on success. Returns -1 on error, with a python exception set.
436 static int
437 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
439 gdb::unique_xmalloc_ptr<char> exp_holder;
440 const char *exp = NULL;
441 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
442 struct gdb_exception except;
444 BPPY_SET_REQUIRE_VALID (self_bp);
446 if (newvalue == NULL)
448 PyErr_SetString (PyExc_TypeError,
449 _("Cannot delete `condition' attribute."));
450 return -1;
452 else if (newvalue == Py_None)
453 exp = "";
454 else
456 exp_holder = python_string_to_host_string (newvalue);
457 if (exp_holder == NULL)
458 return -1;
459 exp = exp_holder.get ();
464 set_breakpoint_condition (self_bp->bp, exp, 0);
466 catch (gdb_exception &ex)
468 except = std::move (ex);
471 GDB_PY_SET_HANDLE_EXCEPTION (except);
473 return 0;
476 /* Python function to get the commands attached to a breakpoint. */
477 static PyObject *
478 bppy_get_commands (PyObject *self, void *closure)
480 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
481 struct breakpoint *bp = self_bp->bp;
483 BPPY_REQUIRE_VALID (self_bp);
485 if (! self_bp->bp->commands)
486 Py_RETURN_NONE;
488 string_file stb;
490 current_uiout->redirect (&stb);
493 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
495 catch (const gdb_exception &except)
497 current_uiout->redirect (NULL);
498 gdbpy_convert_exception (except);
499 return NULL;
502 current_uiout->redirect (NULL);
503 return host_string_to_python_string (stb.c_str ()).release ();
506 /* Set the commands attached to a breakpoint. Returns 0 on success.
507 Returns -1 on error, with a python exception set. */
508 static int
509 bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
511 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
512 struct gdb_exception except;
514 BPPY_SET_REQUIRE_VALID (self_bp);
516 gdb::unique_xmalloc_ptr<char> commands
517 (python_string_to_host_string (newvalue));
518 if (commands == nullptr)
519 return -1;
523 bool first = true;
524 char *save_ptr = nullptr;
525 auto reader
526 = [&] ()
528 const char *result = strtok_r (first ? commands.get () : nullptr,
529 "\n", &save_ptr);
530 first = false;
531 return result;
534 counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
535 breakpoint_set_commands (self_bp->bp, std::move (lines));
537 catch (gdb_exception &ex)
539 except = std::move (ex);
542 GDB_PY_SET_HANDLE_EXCEPTION (except);
544 return 0;
547 /* Python function to get the breakpoint type. */
548 static PyObject *
549 bppy_get_type (PyObject *self, void *closure)
551 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
553 BPPY_REQUIRE_VALID (self_bp);
555 return PyInt_FromLong (self_bp->bp->type);
558 /* Python function to get the visibility of the breakpoint. */
560 static PyObject *
561 bppy_get_visibility (PyObject *self, void *closure)
563 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
565 BPPY_REQUIRE_VALID (self_bp);
567 if (user_breakpoint_p (self_bp->bp))
568 Py_RETURN_TRUE;
570 Py_RETURN_FALSE;
573 /* Python function to determine if the breakpoint is a temporary
574 breakpoint. */
576 static PyObject *
577 bppy_get_temporary (PyObject *self, void *closure)
579 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
581 BPPY_REQUIRE_VALID (self_bp);
583 if (self_bp->bp->disposition == disp_del
584 || self_bp->bp->disposition == disp_del_at_next_stop)
585 Py_RETURN_TRUE;
587 Py_RETURN_FALSE;
590 /* Python function to determine if the breakpoint is a pending
591 breakpoint. */
593 static PyObject *
594 bppy_get_pending (PyObject *self, void *closure)
596 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
598 BPPY_REQUIRE_VALID (self_bp);
600 if (is_watchpoint (self_bp->bp))
601 Py_RETURN_FALSE;
602 if (pending_breakpoint_p (self_bp->bp))
603 Py_RETURN_TRUE;
605 Py_RETURN_FALSE;
608 /* Python function to get the breakpoint's number. */
609 static PyObject *
610 bppy_get_number (PyObject *self, void *closure)
612 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
614 BPPY_REQUIRE_VALID (self_bp);
616 return PyInt_FromLong (self_bp->number);
619 /* Python function to get the breakpoint's thread ID. */
620 static PyObject *
621 bppy_get_thread (PyObject *self, void *closure)
623 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
625 BPPY_REQUIRE_VALID (self_bp);
627 if (self_bp->bp->thread == -1)
628 Py_RETURN_NONE;
630 return PyInt_FromLong (self_bp->bp->thread);
633 /* Python function to get the breakpoint's task ID (in Ada). */
634 static PyObject *
635 bppy_get_task (PyObject *self, void *closure)
637 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
639 BPPY_REQUIRE_VALID (self_bp);
641 if (self_bp->bp->task == 0)
642 Py_RETURN_NONE;
644 return PyInt_FromLong (self_bp->bp->task);
647 /* Python function to get the breakpoint's hit count. */
648 static PyObject *
649 bppy_get_hit_count (PyObject *self, void *closure)
651 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
653 BPPY_REQUIRE_VALID (self_bp);
655 return PyInt_FromLong (self_bp->bp->hit_count);
658 /* Python function to get the breakpoint's ignore count. */
659 static PyObject *
660 bppy_get_ignore_count (PyObject *self, void *closure)
662 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
664 BPPY_REQUIRE_VALID (self_bp);
666 return PyInt_FromLong (self_bp->bp->ignore_count);
669 /* Internal function to validate the Python parameters/keywords
670 provided to bppy_init. */
672 static int
673 bppy_init_validate_args (const char *spec, char *source,
674 char *function, char *label,
675 char *line, enum bptype type)
677 /* If spec is defined, ensure that none of the explicit location
678 keywords are also defined. */
679 if (spec != NULL)
681 if (source != NULL || function != NULL || label != NULL || line != NULL)
683 PyErr_SetString (PyExc_RuntimeError,
684 _("Breakpoints specified with spec cannot "
685 "have source, function, label or line defined."));
686 return -1;
689 else
691 /* If spec isn't defined, ensure that the user is not trying to
692 define a watchpoint with an explicit location. */
693 if (type == bp_watchpoint)
695 PyErr_SetString (PyExc_RuntimeError,
696 _("Watchpoints cannot be set by explicit "
697 "location parameters."));
698 return -1;
700 else
702 /* Otherwise, ensure some explicit locations are defined. */
703 if (source == NULL && function == NULL && label == NULL
704 && line == NULL)
706 PyErr_SetString (PyExc_RuntimeError,
707 _("Neither spec nor explicit location set."));
708 return -1;
710 /* Finally, if source is specified, ensure that line, label
711 or function are specified too. */
712 if (source != NULL && function == NULL && label == NULL
713 && line == NULL)
715 PyErr_SetString (PyExc_RuntimeError,
716 _("Specifying a source must also include a "
717 "line, label or function."));
718 return -1;
722 return 1;
725 /* Python function to create a new breakpoint. */
726 static int
727 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
729 static const char *keywords[] = { "spec", "type", "wp_class", "internal",
730 "temporary","source", "function",
731 "label", "line", "qualified", NULL };
732 const char *spec = NULL;
733 enum bptype type = bp_breakpoint;
734 int access_type = hw_write;
735 PyObject *internal = NULL;
736 PyObject *temporary = NULL;
737 PyObject *lineobj = NULL;;
738 int internal_bp = 0;
739 int temporary_bp = 0;
740 gdb::unique_xmalloc_ptr<char> line;
741 char *label = NULL;
742 char *source = NULL;
743 char *function = NULL;
744 PyObject * qualified = NULL;
746 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
747 &spec, &type, &access_type,
748 &internal,
749 &temporary, &source,
750 &function, &label, &lineobj,
751 &qualified))
752 return -1;
755 if (lineobj != NULL)
757 if (PyInt_Check (lineobj))
758 line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
759 else if (PyString_Check (lineobj))
760 line = python_string_to_host_string (lineobj);
761 else
763 PyErr_SetString (PyExc_RuntimeError,
764 _("Line keyword should be an integer or a string. "));
765 return -1;
769 if (internal)
771 internal_bp = PyObject_IsTrue (internal);
772 if (internal_bp == -1)
773 return -1;
776 if (temporary != NULL)
778 temporary_bp = PyObject_IsTrue (temporary);
779 if (temporary_bp == -1)
780 return -1;
783 if (bppy_init_validate_args (spec, source, function, label, line.get (),
784 type) == -1)
785 return -1;
787 bppy_pending_object = (gdbpy_breakpoint_object *) self;
788 bppy_pending_object->number = -1;
789 bppy_pending_object->bp = NULL;
793 switch (type)
795 case bp_breakpoint:
797 event_location_up location;
798 symbol_name_match_type func_name_match_type
799 = (qualified != NULL && PyObject_IsTrue (qualified)
800 ? symbol_name_match_type::FULL
801 : symbol_name_match_type::WILD);
803 if (spec != NULL)
805 gdb::unique_xmalloc_ptr<char>
806 copy_holder (xstrdup (skip_spaces (spec)));
807 const char *copy = copy_holder.get ();
809 location = string_to_event_location (&copy,
810 current_language,
811 func_name_match_type);
813 else
815 struct explicit_location explicit_loc;
817 initialize_explicit_location (&explicit_loc);
818 explicit_loc.source_filename = source;
819 explicit_loc.function_name = function;
820 explicit_loc.label_name = label;
822 if (line != NULL)
823 explicit_loc.line_offset =
824 linespec_parse_line_offset (line.get ());
826 explicit_loc.func_name_match_type = func_name_match_type;
828 location = new_explicit_location (&explicit_loc);
831 const struct breakpoint_ops *ops =
832 breakpoint_ops_for_event_location (location.get (), false);
834 create_breakpoint (python_gdbarch,
835 location.get (), NULL, -1, NULL,
837 temporary_bp, bp_breakpoint,
839 AUTO_BOOLEAN_TRUE,
840 ops,
841 0, 1, internal_bp, 0);
842 break;
844 case bp_watchpoint:
846 gdb::unique_xmalloc_ptr<char>
847 copy_holder (xstrdup (skip_spaces (spec)));
848 char *copy = copy_holder.get ();
850 if (access_type == hw_write)
851 watch_command_wrapper (copy, 0, internal_bp);
852 else if (access_type == hw_access)
853 awatch_command_wrapper (copy, 0, internal_bp);
854 else if (access_type == hw_read)
855 rwatch_command_wrapper (copy, 0, internal_bp);
856 else
857 error(_("Cannot understand watchpoint access type."));
858 break;
860 default:
861 error(_("Do not understand breakpoint type to set."));
864 catch (const gdb_exception &except)
866 bppy_pending_object = NULL;
867 gdbpy_convert_exception (except);
868 return -1;
871 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
872 return 0;
877 static bool
878 build_bp_list (struct breakpoint *b, PyObject *list)
880 PyObject *bp = (PyObject *) b->py_bp_object;
881 int iserr = 0;
883 /* Not all breakpoints will have a companion Python object.
884 Only breakpoints that were created via bppy_new, or
885 breakpoints that were created externally and are tracked by
886 the Python Scripting API. */
887 if (bp)
888 iserr = PyList_Append (list, bp);
890 if (iserr == -1)
891 return true;
893 return false;
896 /* Static function to return a tuple holding all breakpoints. */
898 PyObject *
899 gdbpy_breakpoints (PyObject *self, PyObject *args)
901 if (bppy_live == 0)
902 return PyTuple_New (0);
904 gdbpy_ref<> list (PyList_New (0));
905 if (list == NULL)
906 return NULL;
908 /* If iterate_over_breakpoints returns non NULL it signals an error
909 condition. In that case abandon building the list and return
910 NULL. */
911 auto callback = [&] (breakpoint *bp)
913 return build_bp_list(bp, list.get ());
915 if (iterate_over_breakpoints (callback) != NULL)
916 return NULL;
918 return PyList_AsTuple (list.get ());
921 /* Call the "stop" method (if implemented) in the breakpoint
922 class. If the method returns True, the inferior will be
923 stopped at the breakpoint. Otherwise the inferior will be
924 allowed to continue. */
926 enum ext_lang_bp_stop
927 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
928 struct breakpoint *b)
930 int stop;
931 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
932 PyObject *py_bp = (PyObject *) bp_obj;
933 struct gdbarch *garch;
935 if (bp_obj == NULL)
936 return EXT_LANG_BP_STOP_UNSET;
938 stop = -1;
939 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
941 gdbpy_enter enter_py (garch, current_language);
943 if (bp_obj->is_finish_bp)
944 bpfinishpy_pre_stop_hook (bp_obj);
946 if (PyObject_HasAttrString (py_bp, stop_func))
948 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
950 stop = 1;
951 if (result != NULL)
953 int evaluate = PyObject_IsTrue (result.get ());
955 if (evaluate == -1)
956 gdbpy_print_stack ();
958 /* If the "stop" function returns False that means
959 the Python breakpoint wants GDB to continue. */
960 if (! evaluate)
961 stop = 0;
963 else
964 gdbpy_print_stack ();
967 if (bp_obj->is_finish_bp)
968 bpfinishpy_post_stop_hook (bp_obj);
970 if (stop < 0)
971 return EXT_LANG_BP_STOP_UNSET;
972 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
975 /* Checks if the "stop" method exists in this breakpoint.
976 Used by condition_command to ensure mutual exclusion of breakpoint
977 conditions. */
980 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
981 struct breakpoint *b)
983 PyObject *py_bp;
984 struct gdbarch *garch;
986 if (b->py_bp_object == NULL)
987 return 0;
989 py_bp = (PyObject *) b->py_bp_object;
990 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
992 gdbpy_enter enter_py (garch, current_language);
993 return PyObject_HasAttrString (py_bp, stop_func);
998 /* Event callback functions. */
1000 /* Callback that is used when a breakpoint is created. This function
1001 will create a new Python breakpoint object. */
1002 static void
1003 gdbpy_breakpoint_created (struct breakpoint *bp)
1005 gdbpy_breakpoint_object *newbp;
1007 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
1008 return;
1010 if (bp->type != bp_breakpoint
1011 && bp->type != bp_watchpoint
1012 && bp->type != bp_hardware_watchpoint
1013 && bp->type != bp_read_watchpoint
1014 && bp->type != bp_access_watchpoint)
1015 return;
1017 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1018 gdbpy_enter enter_py (garch, current_language);
1020 if (bppy_pending_object)
1022 newbp = bppy_pending_object;
1023 Py_INCREF (newbp);
1024 bppy_pending_object = NULL;
1026 else
1027 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
1028 if (newbp)
1030 newbp->number = bp->number;
1031 newbp->bp = bp;
1032 newbp->bp->py_bp_object = newbp;
1033 newbp->is_finish_bp = 0;
1034 ++bppy_live;
1036 else
1038 PyErr_SetString (PyExc_RuntimeError,
1039 _("Error while creating breakpoint from GDB."));
1040 gdbpy_print_stack ();
1043 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1045 if (evpy_emit_event ((PyObject *) newbp,
1046 gdb_py_events.breakpoint_created) < 0)
1047 gdbpy_print_stack ();
1051 /* Callback that is used when a breakpoint is deleted. This will
1052 invalidate the corresponding Python object. */
1053 static void
1054 gdbpy_breakpoint_deleted (struct breakpoint *b)
1056 int num = b->number;
1057 struct breakpoint *bp = NULL;
1059 bp = get_breakpoint (num);
1060 if (bp)
1062 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1063 gdbpy_enter enter_py (garch, current_language);
1065 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1066 if (bp_obj != NULL)
1068 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1070 if (evpy_emit_event ((PyObject *) bp_obj.get (),
1071 gdb_py_events.breakpoint_deleted) < 0)
1072 gdbpy_print_stack ();
1075 bp_obj->bp = NULL;
1076 --bppy_live;
1081 /* Callback that is used when a breakpoint is modified. */
1083 static void
1084 gdbpy_breakpoint_modified (struct breakpoint *b)
1086 int num = b->number;
1087 struct breakpoint *bp = NULL;
1089 bp = get_breakpoint (num);
1090 if (bp)
1092 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1093 gdbpy_enter enter_py (garch, current_language);
1095 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1096 if (bp_obj)
1098 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1100 if (evpy_emit_event (bp_obj,
1101 gdb_py_events.breakpoint_modified) < 0)
1102 gdbpy_print_stack ();
1110 /* Initialize the Python breakpoint code. */
1112 gdbpy_initialize_breakpoints (void)
1114 int i;
1116 breakpoint_object_type.tp_new = PyType_GenericNew;
1117 if (PyType_Ready (&breakpoint_object_type) < 0)
1118 return -1;
1120 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1121 (PyObject *) &breakpoint_object_type) < 0)
1122 return -1;
1124 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created);
1125 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted);
1126 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified);
1128 /* Add breakpoint types constants. */
1129 for (i = 0; pybp_codes[i].name; ++i)
1131 if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
1132 pybp_codes[i].code) < 0)
1133 return -1;
1136 /* Add watchpoint types constants. */
1137 for (i = 0; pybp_watch_types[i].name; ++i)
1139 if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
1140 pybp_watch_types[i].code) < 0)
1141 return -1;
1144 return 0;
1149 /* Helper function that overrides this Python object's
1150 PyObject_GenericSetAttr to allow extra validation of the attribute
1151 being set. */
1153 static int
1154 local_setattro (PyObject *self, PyObject *name, PyObject *v)
1156 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1157 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
1159 if (attr == NULL)
1160 return -1;
1162 /* If the attribute trying to be set is the "stop" method,
1163 but we already have a condition set in the CLI or other extension
1164 language, disallow this operation. */
1165 if (strcmp (attr.get (), stop_func) == 0)
1167 const struct extension_language_defn *extlang = NULL;
1169 if (obj->bp->cond_string != NULL)
1170 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1171 if (extlang == NULL)
1172 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1173 if (extlang != NULL)
1175 std::string error_text
1176 = string_printf (_("Only one stop condition allowed. There is"
1177 " currently a %s stop condition defined for"
1178 " this breakpoint."),
1179 ext_lang_capitalized_name (extlang));
1180 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
1181 return -1;
1185 return PyObject_GenericSetAttr (self, name, v);
1188 static gdb_PyGetSetDef breakpoint_object_getset[] = {
1189 { "enabled", bppy_get_enabled, bppy_set_enabled,
1190 "Boolean telling whether the breakpoint is enabled.", NULL },
1191 { "silent", bppy_get_silent, bppy_set_silent,
1192 "Boolean telling whether the breakpoint is silent.", NULL },
1193 { "thread", bppy_get_thread, bppy_set_thread,
1194 "Thread ID for the breakpoint.\n\
1195 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1196 If the value is None, then this breakpoint is not thread-specific.\n\
1197 No other type of value can be used.", NULL },
1198 { "task", bppy_get_task, bppy_set_task,
1199 "Thread ID for the breakpoint.\n\
1200 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1201 If the value is None, then this breakpoint is not task-specific.\n\
1202 No other type of value can be used.", NULL },
1203 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1204 "Number of times this breakpoint should be automatically continued.",
1205 NULL },
1206 { "number", bppy_get_number, NULL,
1207 "Breakpoint's number assigned by GDB.", NULL },
1208 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1209 "Number of times the breakpoint has been hit.\n\
1210 Can be set to zero to clear the count. No other value is valid\n\
1211 when setting this property.", NULL },
1212 { "location", bppy_get_location, NULL,
1213 "Location of the breakpoint, as specified by the user.", NULL},
1214 { "expression", bppy_get_expression, NULL,
1215 "Expression of the breakpoint, as specified by the user.", NULL},
1216 { "condition", bppy_get_condition, bppy_set_condition,
1217 "Condition of the breakpoint, as specified by the user,\
1218 or None if no condition set."},
1219 { "commands", bppy_get_commands, bppy_set_commands,
1220 "Commands of the breakpoint, as specified by the user."},
1221 { "type", bppy_get_type, NULL,
1222 "Type of breakpoint."},
1223 { "visible", bppy_get_visibility, NULL,
1224 "Whether the breakpoint is visible to the user."},
1225 { "temporary", bppy_get_temporary, NULL,
1226 "Whether this breakpoint is a temporary breakpoint."},
1227 { "pending", bppy_get_pending, NULL,
1228 "Whether this breakpoint is a pending breakpoint."},
1229 { NULL } /* Sentinel. */
1232 static PyMethodDef breakpoint_object_methods[] =
1234 { "is_valid", bppy_is_valid, METH_NOARGS,
1235 "Return true if this breakpoint is valid, false if not." },
1236 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1237 "Delete the underlying GDB breakpoint." },
1238 { NULL } /* Sentinel. */
1241 PyTypeObject breakpoint_object_type =
1243 PyVarObject_HEAD_INIT (NULL, 0)
1244 "gdb.Breakpoint", /*tp_name*/
1245 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1246 0, /*tp_itemsize*/
1247 0, /*tp_dealloc*/
1248 0, /*tp_print*/
1249 0, /*tp_getattr*/
1250 0, /*tp_setattr*/
1251 0, /*tp_compare*/
1252 0, /*tp_repr*/
1253 0, /*tp_as_number*/
1254 0, /*tp_as_sequence*/
1255 0, /*tp_as_mapping*/
1256 0, /*tp_hash */
1257 0, /*tp_call*/
1258 0, /*tp_str*/
1259 0, /*tp_getattro*/
1260 (setattrofunc)local_setattro, /*tp_setattro */
1261 0, /*tp_as_buffer*/
1262 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1263 "GDB breakpoint object", /* tp_doc */
1264 0, /* tp_traverse */
1265 0, /* tp_clear */
1266 0, /* tp_richcompare */
1267 0, /* tp_weaklistoffset */
1268 0, /* tp_iter */
1269 0, /* tp_iternext */
1270 breakpoint_object_methods, /* tp_methods */
1271 0, /* tp_members */
1272 breakpoint_object_getset, /* tp_getset */
1273 0, /* tp_base */
1274 0, /* tp_dict */
1275 0, /* tp_descr_get */
1276 0, /* tp_descr_set */
1277 0, /* tp_dictoffset */
1278 bppy_init, /* tp_init */
1279 0, /* tp_alloc */