gdb: remove iterate_over_breakpoints function
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
1 /* Python interface to breakpoints
2
3 Copyright (C) 2008-2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
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
37 /* Debugging of Python breakpoints. */
38
39 static bool pybp_debug;
40
41 /* Implementation of "show debug py-breakpoint". */
42
43 static void
44 show_pybp_debug (struct ui_file *file, int from_tty,
45 struct cmd_list_element *c, const char *value)
46 {
47 fprintf_filtered (file, _("Python breakpoint debugging is %s.\n"), value);
48 }
49
50 /* Print a "py-breakpoint" debug statement. */
51
52 #define pybp_debug_printf(fmt, ...) \
53 debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__)
54
55 /* Print a "py-breakpoint" enter/exit debug statements. */
56
57 #define PYBP_SCOPED_DEBUG_ENTER_EXIT \
58 scoped_debug_enter_exit (pybp_debug, "py-breakpoint")
59
60 /* Number of live breakpoints. */
61 static int bppy_live;
62
63 /* Variables used to pass information between the Breakpoint
64 constructor and the breakpoint-created hook function. */
65 gdbpy_breakpoint_object *bppy_pending_object;
66
67 /* Function that is called when a Python condition is evaluated. */
68 static const char stop_func[] = "stop";
69
70 /* This is used to initialize various gdb.bp_* constants. */
71 struct pybp_code
72 {
73 /* The name. */
74 const char *name;
75 /* The code. */
76 int code;
77 };
78
79 /* Entries related to the type of user set breakpoints. */
80 static struct pybp_code pybp_codes[] =
81 {
82 { "BP_NONE", bp_none},
83 { "BP_BREAKPOINT", bp_breakpoint},
84 { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint},
85 { "BP_WATCHPOINT", bp_watchpoint},
86 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
87 { "BP_READ_WATCHPOINT", bp_read_watchpoint},
88 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
89 {NULL} /* Sentinel. */
90 };
91
92 /* Entries related to the type of watchpoint. */
93 static struct pybp_code pybp_watch_types[] =
94 {
95 { "WP_READ", hw_read},
96 { "WP_WRITE", hw_write},
97 { "WP_ACCESS", hw_access},
98 {NULL} /* Sentinel. */
99 };
100
101 /* Python function which checks the validity of a breakpoint object. */
102 static PyObject *
103 bppy_is_valid (PyObject *self, PyObject *args)
104 {
105 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
106
107 if (self_bp->bp)
108 Py_RETURN_TRUE;
109 Py_RETURN_FALSE;
110 }
111
112 /* Python function to test whether or not the breakpoint is enabled. */
113 static PyObject *
114 bppy_get_enabled (PyObject *self, void *closure)
115 {
116 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
117
118 BPPY_REQUIRE_VALID (self_bp);
119 if (! self_bp->bp)
120 Py_RETURN_FALSE;
121 if (self_bp->bp->enable_state == bp_enabled)
122 Py_RETURN_TRUE;
123 Py_RETURN_FALSE;
124 }
125
126 /* Python function to test whether or not the breakpoint is silent. */
127 static PyObject *
128 bppy_get_silent (PyObject *self, void *closure)
129 {
130 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
131
132 BPPY_REQUIRE_VALID (self_bp);
133 if (self_bp->bp->silent)
134 Py_RETURN_TRUE;
135 Py_RETURN_FALSE;
136 }
137
138 /* Python function to set the enabled state of a breakpoint. */
139 static int
140 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
141 {
142 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
143 int cmp;
144
145 BPPY_SET_REQUIRE_VALID (self_bp);
146
147 if (newvalue == NULL)
148 {
149 PyErr_SetString (PyExc_TypeError,
150 _("Cannot delete `enabled' attribute."));
151
152 return -1;
153 }
154 else if (! PyBool_Check (newvalue))
155 {
156 PyErr_SetString (PyExc_TypeError,
157 _("The value of `enabled' must be a boolean."));
158 return -1;
159 }
160
161 cmp = PyObject_IsTrue (newvalue);
162 if (cmp < 0)
163 return -1;
164
165 try
166 {
167 if (cmp == 1)
168 enable_breakpoint (self_bp->bp);
169 else
170 disable_breakpoint (self_bp->bp);
171 }
172 catch (const gdb_exception &except)
173 {
174 GDB_PY_SET_HANDLE_EXCEPTION (except);
175 }
176
177 return 0;
178 }
179
180 /* Python function to set the 'silent' state of a breakpoint. */
181 static int
182 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
183 {
184 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
185 int cmp;
186
187 BPPY_SET_REQUIRE_VALID (self_bp);
188
189 if (newvalue == NULL)
190 {
191 PyErr_SetString (PyExc_TypeError,
192 _("Cannot delete `silent' attribute."));
193 return -1;
194 }
195 else if (! PyBool_Check (newvalue))
196 {
197 PyErr_SetString (PyExc_TypeError,
198 _("The value of `silent' must be a boolean."));
199 return -1;
200 }
201
202 cmp = PyObject_IsTrue (newvalue);
203 if (cmp < 0)
204 return -1;
205 else
206 breakpoint_set_silent (self_bp->bp, cmp);
207
208 return 0;
209 }
210
211 /* Python function to set the thread of a breakpoint. */
212 static int
213 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
214 {
215 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
216 long id;
217
218 BPPY_SET_REQUIRE_VALID (self_bp);
219
220 if (newvalue == NULL)
221 {
222 PyErr_SetString (PyExc_TypeError,
223 _("Cannot delete `thread' attribute."));
224 return -1;
225 }
226 else if (PyInt_Check (newvalue))
227 {
228 if (! gdb_py_int_as_long (newvalue, &id))
229 return -1;
230
231 if (!valid_global_thread_id (id))
232 {
233 PyErr_SetString (PyExc_RuntimeError,
234 _("Invalid thread ID."));
235 return -1;
236 }
237 }
238 else if (newvalue == Py_None)
239 id = -1;
240 else
241 {
242 PyErr_SetString (PyExc_TypeError,
243 _("The value of `thread' must be an integer or None."));
244 return -1;
245 }
246
247 breakpoint_set_thread (self_bp->bp, id);
248
249 return 0;
250 }
251
252 /* Python function to set the (Ada) task of a breakpoint. */
253 static int
254 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
255 {
256 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
257 long id;
258 int valid_id = 0;
259
260 BPPY_SET_REQUIRE_VALID (self_bp);
261
262 if (newvalue == NULL)
263 {
264 PyErr_SetString (PyExc_TypeError,
265 _("Cannot delete `task' attribute."));
266 return -1;
267 }
268 else if (PyInt_Check (newvalue))
269 {
270 if (! gdb_py_int_as_long (newvalue, &id))
271 return -1;
272
273 try
274 {
275 valid_id = valid_task_id (id);
276 }
277 catch (const gdb_exception &except)
278 {
279 GDB_PY_SET_HANDLE_EXCEPTION (except);
280 }
281
282 if (! valid_id)
283 {
284 PyErr_SetString (PyExc_RuntimeError,
285 _("Invalid task ID."));
286 return -1;
287 }
288 }
289 else if (newvalue == Py_None)
290 id = 0;
291 else
292 {
293 PyErr_SetString (PyExc_TypeError,
294 _("The value of `task' must be an integer or None."));
295 return -1;
296 }
297
298 breakpoint_set_task (self_bp->bp, id);
299
300 return 0;
301 }
302
303 /* Python function which deletes the underlying GDB breakpoint. This
304 triggers the breakpoint_deleted observer which will call
305 gdbpy_breakpoint_deleted; that function cleans up the Python
306 sections. */
307
308 static PyObject *
309 bppy_delete_breakpoint (PyObject *self, PyObject *args)
310 {
311 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
312
313 BPPY_REQUIRE_VALID (self_bp);
314
315 try
316 {
317 delete_breakpoint (self_bp->bp);
318 }
319 catch (const gdb_exception &except)
320 {
321 GDB_PY_HANDLE_EXCEPTION (except);
322 }
323
324 Py_RETURN_NONE;
325 }
326
327
328 /* Python function to set the ignore count of a breakpoint. */
329 static int
330 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
331 {
332 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
333 long value;
334
335 BPPY_SET_REQUIRE_VALID (self_bp);
336
337 if (newvalue == NULL)
338 {
339 PyErr_SetString (PyExc_TypeError,
340 _("Cannot delete `ignore_count' attribute."));
341 return -1;
342 }
343 else if (! PyInt_Check (newvalue))
344 {
345 PyErr_SetString (PyExc_TypeError,
346 _("The value of `ignore_count' must be an integer."));
347 return -1;
348 }
349
350 if (! gdb_py_int_as_long (newvalue, &value))
351 return -1;
352
353 if (value < 0)
354 value = 0;
355
356 try
357 {
358 set_ignore_count (self_bp->number, (int) value, 0);
359 }
360 catch (const gdb_exception &except)
361 {
362 GDB_PY_SET_HANDLE_EXCEPTION (except);
363 }
364
365 return 0;
366 }
367
368 /* Python function to set the hit count of a breakpoint. */
369 static int
370 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
371 {
372 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
373
374 BPPY_SET_REQUIRE_VALID (self_bp);
375
376 if (newvalue == NULL)
377 {
378 PyErr_SetString (PyExc_TypeError,
379 _("Cannot delete `hit_count' attribute."));
380 return -1;
381 }
382 else
383 {
384 long value;
385
386 if (! gdb_py_int_as_long (newvalue, &value))
387 return -1;
388
389 if (value != 0)
390 {
391 PyErr_SetString (PyExc_AttributeError,
392 _("The value of `hit_count' must be zero."));
393 return -1;
394 }
395 }
396
397 self_bp->bp->hit_count = 0;
398
399 return 0;
400 }
401
402 /* Python function to get the location of a breakpoint. */
403 static PyObject *
404 bppy_get_location (PyObject *self, void *closure)
405 {
406 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
407
408 BPPY_REQUIRE_VALID (obj);
409
410 if (obj->bp->type != bp_breakpoint
411 && obj->bp->type != bp_hardware_breakpoint)
412 Py_RETURN_NONE;
413
414 const char *str = event_location_to_string (obj->bp->location.get ());
415 if (! str)
416 str = "";
417 return host_string_to_python_string (str).release ();
418 }
419
420 /* Python function to get the breakpoint expression. */
421 static PyObject *
422 bppy_get_expression (PyObject *self, void *closure)
423 {
424 const char *str;
425 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
426 struct watchpoint *wp;
427
428 BPPY_REQUIRE_VALID (obj);
429
430 if (!is_watchpoint (obj->bp))
431 Py_RETURN_NONE;
432
433 wp = (struct watchpoint *) obj->bp;
434
435 str = wp->exp_string;
436 if (! str)
437 str = "";
438
439 return host_string_to_python_string (str).release ();
440 }
441
442 /* Python function to get the condition expression of a breakpoint. */
443 static PyObject *
444 bppy_get_condition (PyObject *self, void *closure)
445 {
446 char *str;
447 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
448
449 BPPY_REQUIRE_VALID (obj);
450
451 str = obj->bp->cond_string;
452 if (! str)
453 Py_RETURN_NONE;
454
455 return host_string_to_python_string (str).release ();
456 }
457
458 /* Returns 0 on success. Returns -1 on error, with a python exception set.
459 */
460
461 static int
462 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
463 {
464 gdb::unique_xmalloc_ptr<char> exp_holder;
465 const char *exp = NULL;
466 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
467 struct gdb_exception except;
468
469 BPPY_SET_REQUIRE_VALID (self_bp);
470
471 if (newvalue == NULL)
472 {
473 PyErr_SetString (PyExc_TypeError,
474 _("Cannot delete `condition' attribute."));
475 return -1;
476 }
477 else if (newvalue == Py_None)
478 exp = "";
479 else
480 {
481 exp_holder = python_string_to_host_string (newvalue);
482 if (exp_holder == NULL)
483 return -1;
484 exp = exp_holder.get ();
485 }
486
487 try
488 {
489 set_breakpoint_condition (self_bp->bp, exp, 0, false);
490 }
491 catch (gdb_exception &ex)
492 {
493 except = std::move (ex);
494 }
495
496 GDB_PY_SET_HANDLE_EXCEPTION (except);
497
498 return 0;
499 }
500
501 /* Python function to get the commands attached to a breakpoint. */
502 static PyObject *
503 bppy_get_commands (PyObject *self, void *closure)
504 {
505 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
506 struct breakpoint *bp = self_bp->bp;
507
508 BPPY_REQUIRE_VALID (self_bp);
509
510 if (! self_bp->bp->commands)
511 Py_RETURN_NONE;
512
513 string_file stb;
514
515 current_uiout->redirect (&stb);
516 try
517 {
518 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
519 }
520 catch (const gdb_exception &except)
521 {
522 current_uiout->redirect (NULL);
523 gdbpy_convert_exception (except);
524 return NULL;
525 }
526
527 current_uiout->redirect (NULL);
528 return host_string_to_python_string (stb.c_str ()).release ();
529 }
530
531 /* Set the commands attached to a breakpoint. Returns 0 on success.
532 Returns -1 on error, with a python exception set. */
533 static int
534 bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
535 {
536 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
537 struct gdb_exception except;
538
539 BPPY_SET_REQUIRE_VALID (self_bp);
540
541 gdb::unique_xmalloc_ptr<char> commands
542 (python_string_to_host_string (newvalue));
543 if (commands == nullptr)
544 return -1;
545
546 try
547 {
548 bool first = true;
549 char *save_ptr = nullptr;
550 auto reader
551 = [&] ()
552 {
553 const char *result = strtok_r (first ? commands.get () : nullptr,
554 "\n", &save_ptr);
555 first = false;
556 return result;
557 };
558
559 counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
560 breakpoint_set_commands (self_bp->bp, std::move (lines));
561 }
562 catch (gdb_exception &ex)
563 {
564 except = std::move (ex);
565 }
566
567 GDB_PY_SET_HANDLE_EXCEPTION (except);
568
569 return 0;
570 }
571
572 /* Python function to get the breakpoint type. */
573 static PyObject *
574 bppy_get_type (PyObject *self, void *closure)
575 {
576 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
577
578 BPPY_REQUIRE_VALID (self_bp);
579
580 return gdb_py_object_from_longest (self_bp->bp->type).release ();
581 }
582
583 /* Python function to get the visibility of the breakpoint. */
584
585 static PyObject *
586 bppy_get_visibility (PyObject *self, void *closure)
587 {
588 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
589
590 BPPY_REQUIRE_VALID (self_bp);
591
592 if (user_breakpoint_p (self_bp->bp))
593 Py_RETURN_TRUE;
594
595 Py_RETURN_FALSE;
596 }
597
598 /* Python function to determine if the breakpoint is a temporary
599 breakpoint. */
600
601 static PyObject *
602 bppy_get_temporary (PyObject *self, void *closure)
603 {
604 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
605
606 BPPY_REQUIRE_VALID (self_bp);
607
608 if (self_bp->bp->disposition == disp_del
609 || self_bp->bp->disposition == disp_del_at_next_stop)
610 Py_RETURN_TRUE;
611
612 Py_RETURN_FALSE;
613 }
614
615 /* Python function to determine if the breakpoint is a pending
616 breakpoint. */
617
618 static PyObject *
619 bppy_get_pending (PyObject *self, void *closure)
620 {
621 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
622
623 BPPY_REQUIRE_VALID (self_bp);
624
625 if (is_watchpoint (self_bp->bp))
626 Py_RETURN_FALSE;
627 if (pending_breakpoint_p (self_bp->bp))
628 Py_RETURN_TRUE;
629
630 Py_RETURN_FALSE;
631 }
632
633 /* Python function to get the breakpoint's number. */
634 static PyObject *
635 bppy_get_number (PyObject *self, void *closure)
636 {
637 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
638
639 BPPY_REQUIRE_VALID (self_bp);
640
641 return gdb_py_object_from_longest (self_bp->number).release ();
642 }
643
644 /* Python function to get the breakpoint's thread ID. */
645 static PyObject *
646 bppy_get_thread (PyObject *self, void *closure)
647 {
648 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
649
650 BPPY_REQUIRE_VALID (self_bp);
651
652 if (self_bp->bp->thread == -1)
653 Py_RETURN_NONE;
654
655 return gdb_py_object_from_longest (self_bp->bp->thread).release ();
656 }
657
658 /* Python function to get the breakpoint's task ID (in Ada). */
659 static PyObject *
660 bppy_get_task (PyObject *self, void *closure)
661 {
662 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
663
664 BPPY_REQUIRE_VALID (self_bp);
665
666 if (self_bp->bp->task == 0)
667 Py_RETURN_NONE;
668
669 return gdb_py_object_from_longest (self_bp->bp->task).release ();
670 }
671
672 /* Python function to get the breakpoint's hit count. */
673 static PyObject *
674 bppy_get_hit_count (PyObject *self, void *closure)
675 {
676 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
677
678 BPPY_REQUIRE_VALID (self_bp);
679
680 return gdb_py_object_from_longest (self_bp->bp->hit_count).release ();
681 }
682
683 /* Python function to get the breakpoint's ignore count. */
684 static PyObject *
685 bppy_get_ignore_count (PyObject *self, void *closure)
686 {
687 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
688
689 BPPY_REQUIRE_VALID (self_bp);
690
691 return gdb_py_object_from_longest (self_bp->bp->ignore_count).release ();
692 }
693
694 /* Internal function to validate the Python parameters/keywords
695 provided to bppy_init. */
696
697 static int
698 bppy_init_validate_args (const char *spec, char *source,
699 char *function, char *label,
700 char *line, enum bptype type)
701 {
702 /* If spec is defined, ensure that none of the explicit location
703 keywords are also defined. */
704 if (spec != NULL)
705 {
706 if (source != NULL || function != NULL || label != NULL || line != NULL)
707 {
708 PyErr_SetString (PyExc_RuntimeError,
709 _("Breakpoints specified with spec cannot "
710 "have source, function, label or line defined."));
711 return -1;
712 }
713 }
714 else
715 {
716 /* If spec isn't defined, ensure that the user is not trying to
717 define a watchpoint with an explicit location. */
718 if (type == bp_watchpoint)
719 {
720 PyErr_SetString (PyExc_RuntimeError,
721 _("Watchpoints cannot be set by explicit "
722 "location parameters."));
723 return -1;
724 }
725 else
726 {
727 /* Otherwise, ensure some explicit locations are defined. */
728 if (source == NULL && function == NULL && label == NULL
729 && line == NULL)
730 {
731 PyErr_SetString (PyExc_RuntimeError,
732 _("Neither spec nor explicit location set."));
733 return -1;
734 }
735 /* Finally, if source is specified, ensure that line, label
736 or function are specified too. */
737 if (source != NULL && function == NULL && label == NULL
738 && line == NULL)
739 {
740 PyErr_SetString (PyExc_RuntimeError,
741 _("Specifying a source must also include a "
742 "line, label or function."));
743 return -1;
744 }
745 }
746 }
747 return 1;
748 }
749
750 /* Python function to create a new breakpoint. */
751 static int
752 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
753 {
754 static const char *keywords[] = { "spec", "type", "wp_class", "internal",
755 "temporary","source", "function",
756 "label", "line", "qualified", NULL };
757 const char *spec = NULL;
758 enum bptype type = bp_breakpoint;
759 int access_type = hw_write;
760 PyObject *internal = NULL;
761 PyObject *temporary = NULL;
762 PyObject *lineobj = NULL;;
763 int internal_bp = 0;
764 int temporary_bp = 0;
765 gdb::unique_xmalloc_ptr<char> line;
766 char *label = NULL;
767 char *source = NULL;
768 char *function = NULL;
769 PyObject * qualified = NULL;
770
771 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
772 &spec, &type, &access_type,
773 &internal,
774 &temporary, &source,
775 &function, &label, &lineobj,
776 &qualified))
777 return -1;
778
779
780 if (lineobj != NULL)
781 {
782 if (PyInt_Check (lineobj))
783 line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
784 else if (PyString_Check (lineobj))
785 line = python_string_to_host_string (lineobj);
786 else
787 {
788 PyErr_SetString (PyExc_RuntimeError,
789 _("Line keyword should be an integer or a string. "));
790 return -1;
791 }
792 }
793
794 if (internal)
795 {
796 internal_bp = PyObject_IsTrue (internal);
797 if (internal_bp == -1)
798 return -1;
799 }
800
801 if (temporary != NULL)
802 {
803 temporary_bp = PyObject_IsTrue (temporary);
804 if (temporary_bp == -1)
805 return -1;
806 }
807
808 if (bppy_init_validate_args (spec, source, function, label, line.get (),
809 type) == -1)
810 return -1;
811
812 bppy_pending_object = (gdbpy_breakpoint_object *) self;
813 bppy_pending_object->number = -1;
814 bppy_pending_object->bp = NULL;
815
816 try
817 {
818 switch (type)
819 {
820 case bp_breakpoint:
821 case bp_hardware_breakpoint:
822 {
823 event_location_up location;
824 symbol_name_match_type func_name_match_type
825 = (qualified != NULL && PyObject_IsTrue (qualified)
826 ? symbol_name_match_type::FULL
827 : symbol_name_match_type::WILD);
828
829 if (spec != NULL)
830 {
831 gdb::unique_xmalloc_ptr<char>
832 copy_holder (xstrdup (skip_spaces (spec)));
833 const char *copy = copy_holder.get ();
834
835 location = string_to_event_location (&copy,
836 current_language,
837 func_name_match_type);
838 }
839 else
840 {
841 struct explicit_location explicit_loc;
842
843 initialize_explicit_location (&explicit_loc);
844 explicit_loc.source_filename = source;
845 explicit_loc.function_name = function;
846 explicit_loc.label_name = label;
847
848 if (line != NULL)
849 explicit_loc.line_offset =
850 linespec_parse_line_offset (line.get ());
851
852 explicit_loc.func_name_match_type = func_name_match_type;
853
854 location = new_explicit_location (&explicit_loc);
855 }
856
857 const struct breakpoint_ops *ops =
858 breakpoint_ops_for_event_location (location.get (), false);
859
860 create_breakpoint (python_gdbarch,
861 location.get (), NULL, -1, NULL, false,
862 0,
863 temporary_bp, type,
864 0,
865 AUTO_BOOLEAN_TRUE,
866 ops,
867 0, 1, internal_bp, 0);
868 break;
869 }
870 case bp_watchpoint:
871 {
872 gdb::unique_xmalloc_ptr<char>
873 copy_holder (xstrdup (skip_spaces (spec)));
874 char *copy = copy_holder.get ();
875
876 if (access_type == hw_write)
877 watch_command_wrapper (copy, 0, internal_bp);
878 else if (access_type == hw_access)
879 awatch_command_wrapper (copy, 0, internal_bp);
880 else if (access_type == hw_read)
881 rwatch_command_wrapper (copy, 0, internal_bp);
882 else
883 error(_("Cannot understand watchpoint access type."));
884 break;
885 }
886 default:
887 error(_("Do not understand breakpoint type to set."));
888 }
889 }
890 catch (const gdb_exception &except)
891 {
892 bppy_pending_object = NULL;
893 gdbpy_convert_exception (except);
894 return -1;
895 }
896
897 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
898 return 0;
899 }
900
901 /* Append to LIST the breakpoint Python object associated to B.
902
903 Return true on success. Return false on failure, with the Python error
904 indicator set. */
905
906 static bool
907 build_bp_list (struct breakpoint *b, PyObject *list)
908 {
909 PyObject *bp = (PyObject *) b->py_bp_object;
910
911 /* Not all breakpoints will have a companion Python object.
912 Only breakpoints that were created via bppy_new, or
913 breakpoints that were created externally and are tracked by
914 the Python Scripting API. */
915 if (bp == nullptr)
916 return true;
917
918 return PyList_Append (list, bp) == 0;
919 }
920
921 /* Static function to return a tuple holding all breakpoints. */
922
923 PyObject *
924 gdbpy_breakpoints (PyObject *self, PyObject *args)
925 {
926 if (bppy_live == 0)
927 return PyTuple_New (0);
928
929 gdbpy_ref<> list (PyList_New (0));
930 if (list == NULL)
931 return NULL;
932
933 /* If build_bp_list returns false, it signals an error condition. In that
934 case abandon building the list and return nullptr. */
935 for (breakpoint *bp : all_breakpoints ())
936 if (!build_bp_list (bp, list.get ()))
937 return nullptr;
938
939 return PyList_AsTuple (list.get ());
940 }
941
942 /* Call the "stop" method (if implemented) in the breakpoint
943 class. If the method returns True, the inferior will be
944 stopped at the breakpoint. Otherwise the inferior will be
945 allowed to continue. */
946
947 enum ext_lang_bp_stop
948 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
949 struct breakpoint *b)
950 {
951 int stop;
952 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
953 PyObject *py_bp = (PyObject *) bp_obj;
954 struct gdbarch *garch;
955
956 if (bp_obj == NULL)
957 return EXT_LANG_BP_STOP_UNSET;
958
959 stop = -1;
960 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
961
962 gdbpy_enter enter_py (garch, current_language);
963
964 if (bp_obj->is_finish_bp)
965 bpfinishpy_pre_stop_hook (bp_obj);
966
967 if (PyObject_HasAttrString (py_bp, stop_func))
968 {
969 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
970
971 stop = 1;
972 if (result != NULL)
973 {
974 int evaluate = PyObject_IsTrue (result.get ());
975
976 if (evaluate == -1)
977 gdbpy_print_stack ();
978
979 /* If the "stop" function returns False that means
980 the Python breakpoint wants GDB to continue. */
981 if (! evaluate)
982 stop = 0;
983 }
984 else
985 gdbpy_print_stack ();
986 }
987
988 if (bp_obj->is_finish_bp)
989 bpfinishpy_post_stop_hook (bp_obj);
990
991 if (stop < 0)
992 return EXT_LANG_BP_STOP_UNSET;
993 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
994 }
995
996 /* Checks if the "stop" method exists in this breakpoint.
997 Used by condition_command to ensure mutual exclusion of breakpoint
998 conditions. */
999
1000 int
1001 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
1002 struct breakpoint *b)
1003 {
1004 PyObject *py_bp;
1005 struct gdbarch *garch;
1006
1007 if (b->py_bp_object == NULL)
1008 return 0;
1009
1010 py_bp = (PyObject *) b->py_bp_object;
1011 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
1012
1013 gdbpy_enter enter_py (garch, current_language);
1014 return PyObject_HasAttrString (py_bp, stop_func);
1015 }
1016
1017 \f
1018
1019 /* Event callback functions. */
1020
1021 /* Callback that is used when a breakpoint is created. This function
1022 will create a new Python breakpoint object. */
1023 static void
1024 gdbpy_breakpoint_created (struct breakpoint *bp)
1025 {
1026 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1027
1028 gdbpy_breakpoint_object *newbp;
1029
1030 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
1031 {
1032 pybp_debug_printf ("not attaching python object to this breakpoint");
1033 return;
1034 }
1035
1036 if (bp->type != bp_breakpoint
1037 && bp->type != bp_hardware_breakpoint
1038 && bp->type != bp_watchpoint
1039 && bp->type != bp_hardware_watchpoint
1040 && bp->type != bp_read_watchpoint
1041 && bp->type != bp_access_watchpoint)
1042 {
1043 pybp_debug_printf ("is not a breakpoint or watchpoint");
1044 return;
1045 }
1046
1047 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1048 gdbpy_enter enter_py (garch, current_language);
1049
1050 if (bppy_pending_object)
1051 {
1052 newbp = bppy_pending_object;
1053 Py_INCREF (newbp);
1054 bppy_pending_object = NULL;
1055 pybp_debug_printf ("attaching existing breakpoint object");
1056 }
1057 else
1058 {
1059 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
1060 pybp_debug_printf ("attaching new breakpoint object");
1061 }
1062 if (newbp)
1063 {
1064 newbp->number = bp->number;
1065 newbp->bp = bp;
1066 newbp->bp->py_bp_object = newbp;
1067 newbp->is_finish_bp = 0;
1068 ++bppy_live;
1069 }
1070 else
1071 {
1072 PyErr_SetString (PyExc_RuntimeError,
1073 _("Error while creating breakpoint from GDB."));
1074 gdbpy_print_stack ();
1075 }
1076
1077 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1078 {
1079 if (evpy_emit_event ((PyObject *) newbp,
1080 gdb_py_events.breakpoint_created) < 0)
1081 gdbpy_print_stack ();
1082 }
1083 }
1084
1085 /* Callback that is used when a breakpoint is deleted. This will
1086 invalidate the corresponding Python object. */
1087 static void
1088 gdbpy_breakpoint_deleted (struct breakpoint *b)
1089 {
1090 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1091
1092 int num = b->number;
1093 struct breakpoint *bp = NULL;
1094
1095 bp = get_breakpoint (num);
1096 if (bp)
1097 {
1098 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1099 gdbpy_enter enter_py (garch, current_language);
1100
1101 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1102 if (bp_obj != NULL)
1103 {
1104 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1105 {
1106 if (evpy_emit_event ((PyObject *) bp_obj.get (),
1107 gdb_py_events.breakpoint_deleted) < 0)
1108 gdbpy_print_stack ();
1109 }
1110
1111 bp_obj->bp = NULL;
1112 --bppy_live;
1113 }
1114 }
1115 }
1116
1117 /* Callback that is used when a breakpoint is modified. */
1118
1119 static void
1120 gdbpy_breakpoint_modified (struct breakpoint *b)
1121 {
1122 PYBP_SCOPED_DEBUG_ENTER_EXIT;
1123
1124 int num = b->number;
1125 struct breakpoint *bp = NULL;
1126
1127 bp = get_breakpoint (num);
1128 if (bp)
1129 {
1130 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1131 gdbpy_enter enter_py (garch, current_language);
1132
1133 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1134 if (bp_obj)
1135 {
1136 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1137 {
1138 if (evpy_emit_event (bp_obj,
1139 gdb_py_events.breakpoint_modified) < 0)
1140 gdbpy_print_stack ();
1141 }
1142 }
1143 }
1144 }
1145
1146 \f
1147
1148 /* Initialize the Python breakpoint code. */
1149 int
1150 gdbpy_initialize_breakpoints (void)
1151 {
1152 int i;
1153
1154 breakpoint_object_type.tp_new = PyType_GenericNew;
1155 if (PyType_Ready (&breakpoint_object_type) < 0)
1156 return -1;
1157
1158 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1159 (PyObject *) &breakpoint_object_type) < 0)
1160 return -1;
1161
1162 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created,
1163 "py-breakpoint");
1164 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted,
1165 "py-breakpoint");
1166 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified,
1167 "py-breakpoint");
1168
1169 /* Add breakpoint types constants. */
1170 for (i = 0; pybp_codes[i].name; ++i)
1171 {
1172 if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
1173 pybp_codes[i].code) < 0)
1174 return -1;
1175 }
1176
1177 /* Add watchpoint types constants. */
1178 for (i = 0; pybp_watch_types[i].name; ++i)
1179 {
1180 if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
1181 pybp_watch_types[i].code) < 0)
1182 return -1;
1183 }
1184
1185 return 0;
1186 }
1187
1188 \f
1189
1190 /* Helper function that overrides this Python object's
1191 PyObject_GenericSetAttr to allow extra validation of the attribute
1192 being set. */
1193
1194 static int
1195 local_setattro (PyObject *self, PyObject *name, PyObject *v)
1196 {
1197 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1198 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
1199
1200 if (attr == NULL)
1201 return -1;
1202
1203 /* If the attribute trying to be set is the "stop" method,
1204 but we already have a condition set in the CLI or other extension
1205 language, disallow this operation. */
1206 if (strcmp (attr.get (), stop_func) == 0)
1207 {
1208 const struct extension_language_defn *extlang = NULL;
1209
1210 if (obj->bp->cond_string != NULL)
1211 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1212 if (extlang == NULL)
1213 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1214 if (extlang != NULL)
1215 {
1216 std::string error_text
1217 = string_printf (_("Only one stop condition allowed. There is"
1218 " currently a %s stop condition defined for"
1219 " this breakpoint."),
1220 ext_lang_capitalized_name (extlang));
1221 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
1222 return -1;
1223 }
1224 }
1225
1226 return PyObject_GenericSetAttr (self, name, v);
1227 }
1228
1229 static gdb_PyGetSetDef breakpoint_object_getset[] = {
1230 { "enabled", bppy_get_enabled, bppy_set_enabled,
1231 "Boolean telling whether the breakpoint is enabled.", NULL },
1232 { "silent", bppy_get_silent, bppy_set_silent,
1233 "Boolean telling whether the breakpoint is silent.", NULL },
1234 { "thread", bppy_get_thread, bppy_set_thread,
1235 "Thread ID for the breakpoint.\n\
1236 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1237 If the value is None, then this breakpoint is not thread-specific.\n\
1238 No other type of value can be used.", NULL },
1239 { "task", bppy_get_task, bppy_set_task,
1240 "Thread ID for the breakpoint.\n\
1241 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1242 If the value is None, then this breakpoint is not task-specific.\n\
1243 No other type of value can be used.", NULL },
1244 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1245 "Number of times this breakpoint should be automatically continued.",
1246 NULL },
1247 { "number", bppy_get_number, NULL,
1248 "Breakpoint's number assigned by GDB.", NULL },
1249 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1250 "Number of times the breakpoint has been hit.\n\
1251 Can be set to zero to clear the count. No other value is valid\n\
1252 when setting this property.", NULL },
1253 { "location", bppy_get_location, NULL,
1254 "Location of the breakpoint, as specified by the user.", NULL},
1255 { "expression", bppy_get_expression, NULL,
1256 "Expression of the breakpoint, as specified by the user.", NULL},
1257 { "condition", bppy_get_condition, bppy_set_condition,
1258 "Condition of the breakpoint, as specified by the user,\
1259 or None if no condition set."},
1260 { "commands", bppy_get_commands, bppy_set_commands,
1261 "Commands of the breakpoint, as specified by the user."},
1262 { "type", bppy_get_type, NULL,
1263 "Type of breakpoint."},
1264 { "visible", bppy_get_visibility, NULL,
1265 "Whether the breakpoint is visible to the user."},
1266 { "temporary", bppy_get_temporary, NULL,
1267 "Whether this breakpoint is a temporary breakpoint."},
1268 { "pending", bppy_get_pending, NULL,
1269 "Whether this breakpoint is a pending breakpoint."},
1270 { NULL } /* Sentinel. */
1271 };
1272
1273 static PyMethodDef breakpoint_object_methods[] =
1274 {
1275 { "is_valid", bppy_is_valid, METH_NOARGS,
1276 "Return true if this breakpoint is valid, false if not." },
1277 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1278 "Delete the underlying GDB breakpoint." },
1279 { NULL } /* Sentinel. */
1280 };
1281
1282 PyTypeObject breakpoint_object_type =
1283 {
1284 PyVarObject_HEAD_INIT (NULL, 0)
1285 "gdb.Breakpoint", /*tp_name*/
1286 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1287 0, /*tp_itemsize*/
1288 0, /*tp_dealloc*/
1289 0, /*tp_print*/
1290 0, /*tp_getattr*/
1291 0, /*tp_setattr*/
1292 0, /*tp_compare*/
1293 0, /*tp_repr*/
1294 0, /*tp_as_number*/
1295 0, /*tp_as_sequence*/
1296 0, /*tp_as_mapping*/
1297 0, /*tp_hash */
1298 0, /*tp_call*/
1299 0, /*tp_str*/
1300 0, /*tp_getattro*/
1301 (setattrofunc)local_setattro, /*tp_setattro */
1302 0, /*tp_as_buffer*/
1303 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1304 "GDB breakpoint object", /* tp_doc */
1305 0, /* tp_traverse */
1306 0, /* tp_clear */
1307 0, /* tp_richcompare */
1308 0, /* tp_weaklistoffset */
1309 0, /* tp_iter */
1310 0, /* tp_iternext */
1311 breakpoint_object_methods, /* tp_methods */
1312 0, /* tp_members */
1313 breakpoint_object_getset, /* tp_getset */
1314 0, /* tp_base */
1315 0, /* tp_dict */
1316 0, /* tp_descr_get */
1317 0, /* tp_descr_set */
1318 0, /* tp_dictoffset */
1319 bppy_init, /* tp_init */
1320 0, /* tp_alloc */
1321 };
1322
1323 void _initialize_py_breakpoint ();
1324 void
1325 _initialize_py_breakpoint ()
1326 {
1327 add_setshow_boolean_cmd
1328 ("py-breakpoint", class_maintenance, &pybp_debug,
1329 _("Set Python breakpoint debugging."),
1330 _("Show Python breakpoint debugging."),
1331 _("When on, Python breakpoint debugging is enabled."),
1332 NULL,
1333 show_pybp_debug,
1334 &setdebuglist, &showdebuglist);
1335 }
This page took 0.074546 seconds and 4 git commands to generate.