py-breakpoint.c revision 1.6 1 /* Python interface to breakpoints
2
3 Copyright (C) 2008-2016 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 "observer.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
36 /* Number of live breakpoints. */
37 static int bppy_live;
38
39 /* Variables used to pass information between the Breakpoint
40 constructor and the breakpoint-created hook function. */
41 gdbpy_breakpoint_object *bppy_pending_object;
42
43 /* Function that is called when a Python condition is evaluated. */
44 static char * const stop_func = "stop";
45
46 /* This is used to initialize various gdb.bp_* constants. */
47 struct pybp_code
48 {
49 /* The name. */
50 const char *name;
51 /* The code. */
52 int code;
53 };
54
55 /* Entries related to the type of user set breakpoints. */
56 static struct pybp_code pybp_codes[] =
57 {
58 { "BP_NONE", bp_none},
59 { "BP_BREAKPOINT", bp_breakpoint},
60 { "BP_WATCHPOINT", bp_watchpoint},
61 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
62 { "BP_READ_WATCHPOINT", bp_read_watchpoint},
63 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
64 {NULL} /* Sentinel. */
65 };
66
67 /* Entries related to the type of watchpoint. */
68 static struct pybp_code pybp_watch_types[] =
69 {
70 { "WP_READ", hw_read},
71 { "WP_WRITE", hw_write},
72 { "WP_ACCESS", hw_access},
73 {NULL} /* Sentinel. */
74 };
75
76 /* Python function which checks the validity of a breakpoint object. */
77 static PyObject *
78 bppy_is_valid (PyObject *self, PyObject *args)
79 {
80 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
81
82 if (self_bp->bp)
83 Py_RETURN_TRUE;
84 Py_RETURN_FALSE;
85 }
86
87 /* Python function to test whether or not the breakpoint is enabled. */
88 static PyObject *
89 bppy_get_enabled (PyObject *self, void *closure)
90 {
91 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
92
93 BPPY_REQUIRE_VALID (self_bp);
94 if (! self_bp->bp)
95 Py_RETURN_FALSE;
96 if (self_bp->bp->enable_state == bp_enabled)
97 Py_RETURN_TRUE;
98 Py_RETURN_FALSE;
99 }
100
101 /* Python function to test whether or not the breakpoint is silent. */
102 static PyObject *
103 bppy_get_silent (PyObject *self, void *closure)
104 {
105 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
106
107 BPPY_REQUIRE_VALID (self_bp);
108 if (self_bp->bp->silent)
109 Py_RETURN_TRUE;
110 Py_RETURN_FALSE;
111 }
112
113 /* Python function to set the enabled state of a breakpoint. */
114 static int
115 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
116 {
117 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
118 int cmp;
119
120 BPPY_SET_REQUIRE_VALID (self_bp);
121
122 if (newvalue == NULL)
123 {
124 PyErr_SetString (PyExc_TypeError,
125 _("Cannot delete `enabled' attribute."));
126
127 return -1;
128 }
129 else if (! PyBool_Check (newvalue))
130 {
131 PyErr_SetString (PyExc_TypeError,
132 _("The value of `enabled' must be a boolean."));
133 return -1;
134 }
135
136 cmp = PyObject_IsTrue (newvalue);
137 if (cmp < 0)
138 return -1;
139
140 TRY
141 {
142 if (cmp == 1)
143 enable_breakpoint (self_bp->bp);
144 else
145 disable_breakpoint (self_bp->bp);
146 }
147 CATCH (except, RETURN_MASK_ALL)
148 {
149 GDB_PY_SET_HANDLE_EXCEPTION (except);
150 }
151 END_CATCH
152
153 return 0;
154 }
155
156 /* Python function to set the 'silent' state of a breakpoint. */
157 static int
158 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
159 {
160 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
161 int cmp;
162
163 BPPY_SET_REQUIRE_VALID (self_bp);
164
165 if (newvalue == NULL)
166 {
167 PyErr_SetString (PyExc_TypeError,
168 _("Cannot delete `silent' attribute."));
169 return -1;
170 }
171 else if (! PyBool_Check (newvalue))
172 {
173 PyErr_SetString (PyExc_TypeError,
174 _("The value of `silent' must be a boolean."));
175 return -1;
176 }
177
178 cmp = PyObject_IsTrue (newvalue);
179 if (cmp < 0)
180 return -1;
181 else
182 breakpoint_set_silent (self_bp->bp, cmp);
183
184 return 0;
185 }
186
187 /* Python function to set the thread of a breakpoint. */
188 static int
189 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
190 {
191 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
192 long id;
193
194 BPPY_SET_REQUIRE_VALID (self_bp);
195
196 if (newvalue == NULL)
197 {
198 PyErr_SetString (PyExc_TypeError,
199 _("Cannot delete `thread' attribute."));
200 return -1;
201 }
202 else if (PyInt_Check (newvalue))
203 {
204 if (! gdb_py_int_as_long (newvalue, &id))
205 return -1;
206
207 if (!valid_global_thread_id (id))
208 {
209 PyErr_SetString (PyExc_RuntimeError,
210 _("Invalid thread ID."));
211 return -1;
212 }
213 }
214 else if (newvalue == Py_None)
215 id = -1;
216 else
217 {
218 PyErr_SetString (PyExc_TypeError,
219 _("The value of `thread' must be an integer or None."));
220 return -1;
221 }
222
223 breakpoint_set_thread (self_bp->bp, id);
224
225 return 0;
226 }
227
228 /* Python function to set the (Ada) task of a breakpoint. */
229 static int
230 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
231 {
232 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
233 long id;
234 int valid_id = 0;
235
236 BPPY_SET_REQUIRE_VALID (self_bp);
237
238 if (newvalue == NULL)
239 {
240 PyErr_SetString (PyExc_TypeError,
241 _("Cannot delete `task' attribute."));
242 return -1;
243 }
244 else if (PyInt_Check (newvalue))
245 {
246 if (! gdb_py_int_as_long (newvalue, &id))
247 return -1;
248
249 TRY
250 {
251 valid_id = valid_task_id (id);
252 }
253 CATCH (except, RETURN_MASK_ALL)
254 {
255 GDB_PY_SET_HANDLE_EXCEPTION (except);
256 }
257 END_CATCH
258
259 if (! valid_id)
260 {
261 PyErr_SetString (PyExc_RuntimeError,
262 _("Invalid task ID."));
263 return -1;
264 }
265 }
266 else if (newvalue == Py_None)
267 id = 0;
268 else
269 {
270 PyErr_SetString (PyExc_TypeError,
271 _("The value of `task' must be an integer or None."));
272 return -1;
273 }
274
275 breakpoint_set_task (self_bp->bp, id);
276
277 return 0;
278 }
279
280 /* Python function which deletes the underlying GDB breakpoint. This
281 triggers the breakpoint_deleted observer which will call
282 gdbpy_breakpoint_deleted; that function cleans up the Python
283 sections. */
284
285 static PyObject *
286 bppy_delete_breakpoint (PyObject *self, PyObject *args)
287 {
288 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
289
290 BPPY_REQUIRE_VALID (self_bp);
291
292 TRY
293 {
294 delete_breakpoint (self_bp->bp);
295 }
296 CATCH (except, RETURN_MASK_ALL)
297 {
298 GDB_PY_HANDLE_EXCEPTION (except);
299 }
300 END_CATCH
301
302 Py_RETURN_NONE;
303 }
304
305
306 /* Python function to set the ignore count of a breakpoint. */
307 static int
308 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
309 {
310 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
311 long value;
312
313 BPPY_SET_REQUIRE_VALID (self_bp);
314
315 if (newvalue == NULL)
316 {
317 PyErr_SetString (PyExc_TypeError,
318 _("Cannot delete `ignore_count' attribute."));
319 return -1;
320 }
321 else if (! PyInt_Check (newvalue))
322 {
323 PyErr_SetString (PyExc_TypeError,
324 _("The value of `ignore_count' must be an integer."));
325 return -1;
326 }
327
328 if (! gdb_py_int_as_long (newvalue, &value))
329 return -1;
330
331 if (value < 0)
332 value = 0;
333
334 TRY
335 {
336 set_ignore_count (self_bp->number, (int) value, 0);
337 }
338 CATCH (except, RETURN_MASK_ALL)
339 {
340 GDB_PY_SET_HANDLE_EXCEPTION (except);
341 }
342 END_CATCH
343
344 return 0;
345 }
346
347 /* Python function to set the hit count of a breakpoint. */
348 static int
349 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
350 {
351 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
352
353 BPPY_SET_REQUIRE_VALID (self_bp);
354
355 if (newvalue == NULL)
356 {
357 PyErr_SetString (PyExc_TypeError,
358 _("Cannot delete `hit_count' attribute."));
359 return -1;
360 }
361 else
362 {
363 long value;
364
365 if (! gdb_py_int_as_long (newvalue, &value))
366 return -1;
367
368 if (value != 0)
369 {
370 PyErr_SetString (PyExc_AttributeError,
371 _("The value of `hit_count' must be zero."));
372 return -1;
373 }
374 }
375
376 self_bp->bp->hit_count = 0;
377
378 return 0;
379 }
380
381 /* Python function to get the location of a breakpoint. */
382 static PyObject *
383 bppy_get_location (PyObject *self, void *closure)
384 {
385 const char *str;
386 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
387
388 BPPY_REQUIRE_VALID (obj);
389
390 if (obj->bp->type != bp_breakpoint)
391 Py_RETURN_NONE;
392
393 str = event_location_to_string (obj->bp->location);
394 if (! str)
395 str = "";
396 return host_string_to_python_string (str);
397 }
398
399 /* Python function to get the breakpoint expression. */
400 static PyObject *
401 bppy_get_expression (PyObject *self, void *closure)
402 {
403 char *str;
404 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
405 struct watchpoint *wp;
406
407 BPPY_REQUIRE_VALID (obj);
408
409 if (!is_watchpoint (obj->bp))
410 Py_RETURN_NONE;
411
412 wp = (struct watchpoint *) obj->bp;
413
414 str = wp->exp_string;
415 if (! str)
416 str = "";
417
418 return host_string_to_python_string (str);
419 }
420
421 /* Python function to get the condition expression of a breakpoint. */
422 static PyObject *
423 bppy_get_condition (PyObject *self, void *closure)
424 {
425 char *str;
426 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
427
428 BPPY_REQUIRE_VALID (obj);
429
430 str = obj->bp->cond_string;
431 if (! str)
432 Py_RETURN_NONE;
433
434 return host_string_to_python_string (str);
435 }
436
437 /* Returns 0 on success. Returns -1 on error, with a python exception set.
438 */
439
440 static int
441 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
442 {
443 char *exp;
444 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
445 struct gdb_exception except = exception_none;
446
447 BPPY_SET_REQUIRE_VALID (self_bp);
448
449 if (newvalue == NULL)
450 {
451 PyErr_SetString (PyExc_TypeError,
452 _("Cannot delete `condition' attribute."));
453 return -1;
454 }
455 else if (newvalue == Py_None)
456 exp = "";
457 else
458 {
459 exp = python_string_to_host_string (newvalue);
460 if (exp == NULL)
461 return -1;
462 }
463
464 TRY
465 {
466 set_breakpoint_condition (self_bp->bp, exp, 0);
467 }
468 CATCH (ex, RETURN_MASK_ALL)
469 {
470 except = ex;
471 }
472 END_CATCH
473
474 if (newvalue != Py_None)
475 xfree (exp);
476
477 GDB_PY_SET_HANDLE_EXCEPTION (except);
478
479 return 0;
480 }
481
482 /* Python function to get the commands attached to a breakpoint. */
483 static PyObject *
484 bppy_get_commands (PyObject *self, void *closure)
485 {
486 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
487 struct breakpoint *bp = self_bp->bp;
488 long length;
489 struct ui_file *string_file;
490 struct cleanup *chain;
491 PyObject *result;
492 char *cmdstr;
493
494 BPPY_REQUIRE_VALID (self_bp);
495
496 if (! self_bp->bp->commands)
497 Py_RETURN_NONE;
498
499 string_file = mem_fileopen ();
500 chain = make_cleanup_ui_file_delete (string_file);
501
502 ui_out_redirect (current_uiout, string_file);
503 TRY
504 {
505 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
506 }
507 CATCH (except, RETURN_MASK_ALL)
508 {
509 ui_out_redirect (current_uiout, NULL);
510 do_cleanups (chain);
511 gdbpy_convert_exception (except);
512 return NULL;
513 }
514 END_CATCH
515
516 ui_out_redirect (current_uiout, NULL);
517 cmdstr = ui_file_xstrdup (string_file, &length);
518 make_cleanup (xfree, cmdstr);
519 result = host_string_to_python_string (cmdstr);
520 do_cleanups (chain);
521 return result;
522 }
523
524 /* Python function to get the breakpoint type. */
525 static PyObject *
526 bppy_get_type (PyObject *self, void *closure)
527 {
528 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
529
530 BPPY_REQUIRE_VALID (self_bp);
531
532 return PyInt_FromLong (self_bp->bp->type);
533 }
534
535 /* Python function to get the visibility of the breakpoint. */
536
537 static PyObject *
538 bppy_get_visibility (PyObject *self, void *closure)
539 {
540 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
541
542 BPPY_REQUIRE_VALID (self_bp);
543
544 if (user_breakpoint_p (self_bp->bp))
545 Py_RETURN_TRUE;
546
547 Py_RETURN_FALSE;
548 }
549
550 /* Python function to determine if the breakpoint is a temporary
551 breakpoint. */
552
553 static PyObject *
554 bppy_get_temporary (PyObject *self, void *closure)
555 {
556 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
557
558 BPPY_REQUIRE_VALID (self_bp);
559
560 if (self_bp->bp->disposition == disp_del
561 || self_bp->bp->disposition == disp_del_at_next_stop)
562 Py_RETURN_TRUE;
563
564 Py_RETURN_FALSE;
565 }
566
567 /* Python function to determine if the breakpoint is a pending
568 breakpoint. */
569
570 static PyObject *
571 bppy_get_pending (PyObject *self, void *closure)
572 {
573 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
574
575 BPPY_REQUIRE_VALID (self_bp);
576
577 if (is_watchpoint (self_bp->bp))
578 Py_RETURN_FALSE;
579 if (pending_breakpoint_p (self_bp->bp))
580 Py_RETURN_TRUE;
581
582 Py_RETURN_FALSE;
583 }
584
585 /* Python function to get the breakpoint's number. */
586 static PyObject *
587 bppy_get_number (PyObject *self, void *closure)
588 {
589 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
590
591 BPPY_REQUIRE_VALID (self_bp);
592
593 return PyInt_FromLong (self_bp->number);
594 }
595
596 /* Python function to get the breakpoint's thread ID. */
597 static PyObject *
598 bppy_get_thread (PyObject *self, void *closure)
599 {
600 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
601
602 BPPY_REQUIRE_VALID (self_bp);
603
604 if (self_bp->bp->thread == -1)
605 Py_RETURN_NONE;
606
607 return PyInt_FromLong (self_bp->bp->thread);
608 }
609
610 /* Python function to get the breakpoint's task ID (in Ada). */
611 static PyObject *
612 bppy_get_task (PyObject *self, void *closure)
613 {
614 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
615
616 BPPY_REQUIRE_VALID (self_bp);
617
618 if (self_bp->bp->task == 0)
619 Py_RETURN_NONE;
620
621 return PyInt_FromLong (self_bp->bp->task);
622 }
623
624 /* Python function to get the breakpoint's hit count. */
625 static PyObject *
626 bppy_get_hit_count (PyObject *self, void *closure)
627 {
628 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
629
630 BPPY_REQUIRE_VALID (self_bp);
631
632 return PyInt_FromLong (self_bp->bp->hit_count);
633 }
634
635 /* Python function to get the breakpoint's ignore count. */
636 static PyObject *
637 bppy_get_ignore_count (PyObject *self, void *closure)
638 {
639 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
640
641 BPPY_REQUIRE_VALID (self_bp);
642
643 return PyInt_FromLong (self_bp->bp->ignore_count);
644 }
645
646 /* Python function to create a new breakpoint. */
647 static int
648 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
649 {
650 static char *keywords[] = { "spec", "type", "wp_class", "internal",
651 "temporary", NULL };
652 const char *spec;
653 int type = bp_breakpoint;
654 int access_type = hw_write;
655 PyObject *internal = NULL;
656 PyObject *temporary = NULL;
657 int internal_bp = 0;
658 int temporary_bp = 0;
659
660 if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
661 &spec, &type, &access_type,
662 &internal, &temporary))
663 return -1;
664
665 if (internal)
666 {
667 internal_bp = PyObject_IsTrue (internal);
668 if (internal_bp == -1)
669 return -1;
670 }
671
672 if (temporary != NULL)
673 {
674 temporary_bp = PyObject_IsTrue (temporary);
675 if (temporary_bp == -1)
676 return -1;
677 }
678
679 bppy_pending_object = (gdbpy_breakpoint_object *) self;
680 bppy_pending_object->number = -1;
681 bppy_pending_object->bp = NULL;
682
683 TRY
684 {
685 char *copy = xstrdup (skip_spaces_const (spec));
686 struct cleanup *cleanup = make_cleanup (xfree, copy);
687
688 switch (type)
689 {
690 case bp_breakpoint:
691 {
692 struct event_location *location;
693
694 location
695 = string_to_event_location_basic (©, current_language);
696 make_cleanup_delete_event_location (location);
697 create_breakpoint (python_gdbarch,
698 location, NULL, -1, NULL,
699 0,
700 temporary_bp, bp_breakpoint,
701 0,
702 AUTO_BOOLEAN_TRUE,
703 &bkpt_breakpoint_ops,
704 0, 1, internal_bp, 0);
705 break;
706 }
707 case bp_watchpoint:
708 {
709 if (access_type == hw_write)
710 watch_command_wrapper (copy, 0, internal_bp);
711 else if (access_type == hw_access)
712 awatch_command_wrapper (copy, 0, internal_bp);
713 else if (access_type == hw_read)
714 rwatch_command_wrapper (copy, 0, internal_bp);
715 else
716 error(_("Cannot understand watchpoint access type."));
717 break;
718 }
719 default:
720 error(_("Do not understand breakpoint type to set."));
721 }
722
723 do_cleanups (cleanup);
724 }
725 CATCH (except, RETURN_MASK_ALL)
726 {
727 bppy_pending_object = NULL;
728 PyErr_Format (except.reason == RETURN_QUIT
729 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
730 "%s", except.message);
731 return -1;
732 }
733 END_CATCH
734
735 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
736 return 0;
737 }
738
739
740
742 static int
743 build_bp_list (struct breakpoint *b, void *arg)
744 {
745 PyObject *list = (PyObject *) arg;
746 PyObject *bp = (PyObject *) b->py_bp_object;
747 int iserr = 0;
748
749 /* Not all breakpoints will have a companion Python object.
750 Only breakpoints that were created via bppy_new, or
751 breakpoints that were created externally and are tracked by
752 the Python Scripting API. */
753 if (bp)
754 iserr = PyList_Append (list, bp);
755
756 if (iserr == -1)
757 return 1;
758
759 return 0;
760 }
761
762 /* Static function to return a tuple holding all breakpoints. */
763
764 PyObject *
765 gdbpy_breakpoints (PyObject *self, PyObject *args)
766 {
767 PyObject *list, *tuple;
768
769 if (bppy_live == 0)
770 return PyTuple_New (0);
771
772 list = PyList_New (0);
773 if (!list)
774 return NULL;
775
776 /* If iterate_over_breakpoints returns non NULL it signals an error
777 condition. In that case abandon building the list and return
778 NULL. */
779 if (iterate_over_breakpoints (build_bp_list, list) != NULL)
780 {
781 Py_DECREF (list);
782 return NULL;
783 }
784
785 tuple = PyList_AsTuple (list);
786 Py_DECREF (list);
787
788 return tuple;
789 }
790
791 /* Call the "stop" method (if implemented) in the breakpoint
792 class. If the method returns True, the inferior will be
793 stopped at the breakpoint. Otherwise the inferior will be
794 allowed to continue. */
795
796 enum ext_lang_bp_stop
797 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
798 struct breakpoint *b)
799 {
800 int stop;
801 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
802 PyObject *py_bp = (PyObject *) bp_obj;
803 struct gdbarch *garch;
804 struct cleanup *cleanup;
805
806 if (bp_obj == NULL)
807 return EXT_LANG_BP_STOP_UNSET;
808
809 stop = -1;
810 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
811 cleanup = ensure_python_env (garch, current_language);
812
813 if (bp_obj->is_finish_bp)
814 bpfinishpy_pre_stop_hook (bp_obj);
815
816 if (PyObject_HasAttrString (py_bp, stop_func))
817 {
818 PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);
819
820 stop = 1;
821 if (result)
822 {
823 int evaluate = PyObject_IsTrue (result);
824
825 if (evaluate == -1)
826 gdbpy_print_stack ();
827
828 /* If the "stop" function returns False that means
829 the Python breakpoint wants GDB to continue. */
830 if (! evaluate)
831 stop = 0;
832
833 Py_DECREF (result);
834 }
835 else
836 gdbpy_print_stack ();
837 }
838
839 if (bp_obj->is_finish_bp)
840 bpfinishpy_post_stop_hook (bp_obj);
841
842 do_cleanups (cleanup);
843
844 if (stop < 0)
845 return EXT_LANG_BP_STOP_UNSET;
846 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
847 }
848
849 /* Checks if the "stop" method exists in this breakpoint.
850 Used by condition_command to ensure mutual exclusion of breakpoint
851 conditions. */
852
853 int
854 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
855 struct breakpoint *b)
856 {
857 int has_func;
858 PyObject *py_bp;
859 struct gdbarch *garch;
860 struct cleanup *cleanup;
861
862 if (b->py_bp_object == NULL)
863 return 0;
864
865 py_bp = (PyObject *) b->py_bp_object;
866 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
867 cleanup = ensure_python_env (garch, current_language);
868 has_func = PyObject_HasAttrString (py_bp, stop_func);
869 do_cleanups (cleanup);
870
871 return has_func;
872 }
873
874
875
877 /* Event callback functions. */
878
879 /* Callback that is used when a breakpoint is created. This function
880 will create a new Python breakpoint object. */
881 static void
882 gdbpy_breakpoint_created (struct breakpoint *bp)
883 {
884 gdbpy_breakpoint_object *newbp;
885 PyGILState_STATE state;
886
887 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
888 return;
889
890 if (bp->type != bp_breakpoint
891 && bp->type != bp_watchpoint
892 && bp->type != bp_hardware_watchpoint
893 && bp->type != bp_read_watchpoint
894 && bp->type != bp_access_watchpoint)
895 return;
896
897 state = PyGILState_Ensure ();
898
899 if (bppy_pending_object)
900 {
901 newbp = bppy_pending_object;
902 bppy_pending_object = NULL;
903 }
904 else
905 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
906 if (newbp)
907 {
908 newbp->number = bp->number;
909 newbp->bp = bp;
910 newbp->bp->py_bp_object = newbp;
911 newbp->is_finish_bp = 0;
912 Py_INCREF (newbp);
913 ++bppy_live;
914 }
915 else
916 {
917 PyErr_SetString (PyExc_RuntimeError,
918 _("Error while creating breakpoint from GDB."));
919 gdbpy_print_stack ();
920 }
921
922 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
923 {
924 Py_INCREF (newbp);
925 if (evpy_emit_event ((PyObject *) newbp,
926 gdb_py_events.breakpoint_created) < 0)
927 gdbpy_print_stack ();
928 }
929
930 PyGILState_Release (state);
931 }
932
933 /* Callback that is used when a breakpoint is deleted. This will
934 invalidate the corresponding Python object. */
935 static void
936 gdbpy_breakpoint_deleted (struct breakpoint *b)
937 {
938 int num = b->number;
939 PyGILState_STATE state;
940 struct breakpoint *bp = NULL;
941 gdbpy_breakpoint_object *bp_obj;
942
943 state = PyGILState_Ensure ();
944 bp = get_breakpoint (num);
945 if (bp)
946 {
947 bp_obj = bp->py_bp_object;
948 if (bp_obj)
949 {
950 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
951 {
952 PyObject *bp_obj_alias = (PyObject *) bp_obj;
953
954 Py_INCREF (bp_obj_alias);
955 if (evpy_emit_event (bp_obj_alias,
956 gdb_py_events.breakpoint_deleted) < 0)
957 gdbpy_print_stack ();
958 }
959
960 bp_obj->bp = NULL;
961 --bppy_live;
962 Py_DECREF (bp_obj);
963 }
964 }
965 PyGILState_Release (state);
966 }
967
968 /* Callback that is used when a breakpoint is modified. */
969
970 static void
971 gdbpy_breakpoint_modified (struct breakpoint *b)
972 {
973 int num = b->number;
974 PyGILState_STATE state;
975 struct breakpoint *bp = NULL;
976 gdbpy_breakpoint_object *bp_obj;
977
978 state = PyGILState_Ensure ();
979 bp = get_breakpoint (num);
980 if (bp)
981 {
982 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
983 if (bp_obj)
984 {
985 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
986 {
987 Py_INCREF (bp_obj);
988 if (evpy_emit_event (bp_obj,
989 gdb_py_events.breakpoint_modified) < 0)
990 gdbpy_print_stack ();
991 }
992 }
993 }
994 PyGILState_Release (state);
995 }
996
997
998
1000 /* Initialize the Python breakpoint code. */
1001 int
1002 gdbpy_initialize_breakpoints (void)
1003 {
1004 int i;
1005
1006 breakpoint_object_type.tp_new = PyType_GenericNew;
1007 if (PyType_Ready (&breakpoint_object_type) < 0)
1008 return -1;
1009
1010 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1011 (PyObject *) &breakpoint_object_type) < 0)
1012 return -1;
1013
1014 observer_attach_breakpoint_created (gdbpy_breakpoint_created);
1015 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
1016 observer_attach_breakpoint_modified (gdbpy_breakpoint_modified);
1017
1018 /* Add breakpoint types constants. */
1019 for (i = 0; pybp_codes[i].name; ++i)
1020 {
1021 if (PyModule_AddIntConstant (gdb_module,
1022 /* Cast needed for Python 2.4. */
1023 (char *) pybp_codes[i].name,
1024 pybp_codes[i].code) < 0)
1025 return -1;
1026 }
1027
1028 /* Add watchpoint types constants. */
1029 for (i = 0; pybp_watch_types[i].name; ++i)
1030 {
1031 if (PyModule_AddIntConstant (gdb_module,
1032 /* Cast needed for Python 2.4. */
1033 (char *) pybp_watch_types[i].name,
1034 pybp_watch_types[i].code) < 0)
1035 return -1;
1036 }
1037
1038 return 0;
1039 }
1040
1041
1042
1044 /* Helper function that overrides this Python object's
1045 PyObject_GenericSetAttr to allow extra validation of the attribute
1046 being set. */
1047
1048 static int
1049 local_setattro (PyObject *self, PyObject *name, PyObject *v)
1050 {
1051 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1052 char *attr = python_string_to_host_string (name);
1053
1054 if (attr == NULL)
1055 return -1;
1056
1057 /* If the attribute trying to be set is the "stop" method,
1058 but we already have a condition set in the CLI or other extension
1059 language, disallow this operation. */
1060 if (strcmp (attr, stop_func) == 0)
1061 {
1062 const struct extension_language_defn *extlang = NULL;
1063
1064 if (obj->bp->cond_string != NULL)
1065 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1066 if (extlang == NULL)
1067 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1068 if (extlang != NULL)
1069 {
1070 char *error_text;
1071
1072 xfree (attr);
1073 error_text
1074 = xstrprintf (_("Only one stop condition allowed. There is"
1075 " currently a %s stop condition defined for"
1076 " this breakpoint."),
1077 ext_lang_capitalized_name (extlang));
1078 PyErr_SetString (PyExc_RuntimeError, error_text);
1079 xfree (error_text);
1080 return -1;
1081 }
1082 }
1083
1084 xfree (attr);
1085
1086 return PyObject_GenericSetAttr ((PyObject *)self, name, v);
1087 }
1088
1089 static PyGetSetDef breakpoint_object_getset[] = {
1090 { "enabled", bppy_get_enabled, bppy_set_enabled,
1091 "Boolean telling whether the breakpoint is enabled.", NULL },
1092 { "silent", bppy_get_silent, bppy_set_silent,
1093 "Boolean telling whether the breakpoint is silent.", NULL },
1094 { "thread", bppy_get_thread, bppy_set_thread,
1095 "Thread ID for the breakpoint.\n\
1096 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1097 If the value is None, then this breakpoint is not thread-specific.\n\
1098 No other type of value can be used.", NULL },
1099 { "task", bppy_get_task, bppy_set_task,
1100 "Thread ID for the breakpoint.\n\
1101 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1102 If the value is None, then this breakpoint is not task-specific.\n\
1103 No other type of value can be used.", NULL },
1104 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1105 "Number of times this breakpoint should be automatically continued.",
1106 NULL },
1107 { "number", bppy_get_number, NULL,
1108 "Breakpoint's number assigned by GDB.", NULL },
1109 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1110 "Number of times the breakpoint has been hit.\n\
1111 Can be set to zero to clear the count. No other value is valid\n\
1112 when setting this property.", NULL },
1113 { "location", bppy_get_location, NULL,
1114 "Location of the breakpoint, as specified by the user.", NULL},
1115 { "expression", bppy_get_expression, NULL,
1116 "Expression of the breakpoint, as specified by the user.", NULL},
1117 { "condition", bppy_get_condition, bppy_set_condition,
1118 "Condition of the breakpoint, as specified by the user,\
1119 or None if no condition set."},
1120 { "commands", bppy_get_commands, NULL,
1121 "Commands of the breakpoint, as specified by the user."},
1122 { "type", bppy_get_type, NULL,
1123 "Type of breakpoint."},
1124 { "visible", bppy_get_visibility, NULL,
1125 "Whether the breakpoint is visible to the user."},
1126 { "temporary", bppy_get_temporary, NULL,
1127 "Whether this breakpoint is a temporary breakpoint."},
1128 { "pending", bppy_get_pending, NULL,
1129 "Whether this breakpoint is a pending breakpoint."},
1130 { NULL } /* Sentinel. */
1131 };
1132
1133 static PyMethodDef breakpoint_object_methods[] =
1134 {
1135 { "is_valid", bppy_is_valid, METH_NOARGS,
1136 "Return true if this breakpoint is valid, false if not." },
1137 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1138 "Delete the underlying GDB breakpoint." },
1139 { NULL } /* Sentinel. */
1140 };
1141
1142 PyTypeObject breakpoint_object_type =
1143 {
1144 PyVarObject_HEAD_INIT (NULL, 0)
1145 "gdb.Breakpoint", /*tp_name*/
1146 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1147 0, /*tp_itemsize*/
1148 0, /*tp_dealloc*/
1149 0, /*tp_print*/
1150 0, /*tp_getattr*/
1151 0, /*tp_setattr*/
1152 0, /*tp_compare*/
1153 0, /*tp_repr*/
1154 0, /*tp_as_number*/
1155 0, /*tp_as_sequence*/
1156 0, /*tp_as_mapping*/
1157 0, /*tp_hash */
1158 0, /*tp_call*/
1159 0, /*tp_str*/
1160 0, /*tp_getattro*/
1161 (setattrofunc)local_setattro, /*tp_setattro */
1162 0, /*tp_as_buffer*/
1163 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1164 "GDB breakpoint object", /* tp_doc */
1165 0, /* tp_traverse */
1166 0, /* tp_clear */
1167 0, /* tp_richcompare */
1168 0, /* tp_weaklistoffset */
1169 0, /* tp_iter */
1170 0, /* tp_iternext */
1171 breakpoint_object_methods, /* tp_methods */
1172 0, /* tp_members */
1173 breakpoint_object_getset, /* tp_getset */
1174 0, /* tp_base */
1175 0, /* tp_dict */
1176 0, /* tp_descr_get */
1177 0, /* tp_descr_set */
1178 0, /* tp_dictoffset */
1179 bppy_init, /* tp_init */
1180 0, /* tp_alloc */
1181 };
1182