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