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