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