Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / python / py-breakpoint.c
CommitLineData
c50c785c
JM
1/* Python interface to breakpoints
2
ef5ccd6c 3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
c50c785c
JM
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"
ef5ccd6c 24#include "python.h"
c50c785c
JM
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
c50c785c
JM
35/* Number of live breakpoints. */
36static int bppy_live;
37
38/* Variables used to pass information between the Breakpoint
39 constructor and the breakpoint-created hook function. */
a45ae5f8 40breakpoint_object *bppy_pending_object;
c50c785c
JM
41
42/* Function that is called when a Python condition is evaluated. */
43static char * const stop_func = "stop";
44
c50c785c
JM
45/* This is used to initialize various gdb.bp_* constants. */
46struct pybp_code
47{
48 /* The name. */
49 const char *name;
50 /* The code. */
ef5ccd6c 51 int code;
c50c785c
JM
52};
53
54/* Entries related to the type of user set breakpoints. */
55static 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. */
67static 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. */
76static PyObject *
77bppy_is_valid (PyObject *self, PyObject *args)
78{
79 breakpoint_object *self_bp = (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. */
87static PyObject *
88bppy_get_enabled (PyObject *self, void *closure)
89{
90 breakpoint_object *self_bp = (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. */
101static PyObject *
102bppy_get_silent (PyObject *self, void *closure)
103{
104 breakpoint_object *self_bp = (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. */
113static int
114bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
115{
116 breakpoint_object *self_bp = (breakpoint_object *) self;
117 int cmp;
a45ae5f8 118 volatile struct gdb_exception except;
c50c785c
JM
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;
a45ae5f8
JM
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
c50c785c
JM
149 return 0;
150}
151
152/* Python function to set the 'silent' state of a breakpoint. */
153static int
154bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
155{
156 breakpoint_object *self_bp = (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. */
184static int
185bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
186{
187 breakpoint_object *self_bp = (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. */
225static int
226bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
227{
228 breakpoint_object *self_bp = (breakpoint_object *) self;
229 long id;
a45ae5f8
JM
230 int valid_id = 0;
231 volatile struct gdb_exception except;
c50c785c
JM
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
a45ae5f8
JM
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)
c50c785c
JM
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
278static PyObject *
279bppy_delete_breakpoint (PyObject *self, PyObject *args)
280{
281 breakpoint_object *self_bp = (breakpoint_object *) self;
a45ae5f8 282 volatile struct gdb_exception except;
c50c785c
JM
283
284 BPPY_REQUIRE_VALID (self_bp);
285
a45ae5f8
JM
286 TRY_CATCH (except, RETURN_MASK_ALL)
287 {
288 delete_breakpoint (self_bp->bp);
289 }
290 GDB_PY_HANDLE_EXCEPTION (except);
c50c785c
JM
291
292 Py_RETURN_NONE;
293}
294
295
296/* Python function to set the ignore count of a breakpoint. */
297static int
298bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
299{
300 breakpoint_object *self_bp = (breakpoint_object *) self;
301 long value;
a45ae5f8 302 volatile struct gdb_exception except;
c50c785c
JM
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;
a45ae5f8
JM
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);
c50c785c
JM
330
331 return 0;
332}
333
334/* Python function to set the hit count of a breakpoint. */
335static int
336bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
337{
338 breakpoint_object *self_bp = (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. */
369static PyObject *
370bppy_get_location (PyObject *self, void *closure)
371{
372 char *str;
373 breakpoint_object *obj = (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. */
388static PyObject *
389bppy_get_expression (PyObject *self, void *closure)
390{
391 char *str;
392 breakpoint_object *obj = (breakpoint_object *) self;
a45ae5f8 393 struct watchpoint *wp;
c50c785c
JM
394
395 BPPY_REQUIRE_VALID (obj);
396
a45ae5f8 397 if (!is_watchpoint (obj->bp))
c50c785c
JM
398 Py_RETURN_NONE;
399
a45ae5f8
JM
400 wp = (struct watchpoint *) obj->bp;
401
402 str = wp->exp_string;
c50c785c
JM
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. */
410static PyObject *
411bppy_get_condition (PyObject *self, void *closure)
412{
413 char *str;
414 breakpoint_object *obj = (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
428static int
429bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
430{
431 char *exp;
432 breakpoint_object *self_bp = (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. */
466static PyObject *
467bppy_get_commands (PyObject *self, void *closure)
468{
469 breakpoint_object *self_bp = (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
a45ae5f8 486 ui_out_redirect (current_uiout, string_file);
c50c785c
JM
487 TRY_CATCH (except, RETURN_MASK_ALL)
488 {
a45ae5f8 489 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
c50c785c 490 }
a45ae5f8 491 ui_out_redirect (current_uiout, NULL);
c50c785c
JM
492 GDB_PY_HANDLE_EXCEPTION (except);
493
494 cmdstr = ui_file_xstrdup (string_file, &length);
495 make_cleanup (xfree, cmdstr);
496 result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL);
497 do_cleanups (chain);
498 return result;
499}
500
501/* Python function to get the breakpoint type. */
502static PyObject *
503bppy_get_type (PyObject *self, void *closure)
504{
505 breakpoint_object *self_bp = (breakpoint_object *) self;
506
507 BPPY_REQUIRE_VALID (self_bp);
508
509 return PyInt_FromLong (self_bp->bp->type);
510}
511
512/* Python function to get the visibility of the breakpoint. */
513
514static PyObject *
515bppy_get_visibility (PyObject *self, void *closure)
516{
517 breakpoint_object *self_bp = (breakpoint_object *) self;
518
519 BPPY_REQUIRE_VALID (self_bp);
520
521 if (self_bp->bp->number < 0)
522 Py_RETURN_FALSE;
523
524 Py_RETURN_TRUE;
525}
526
527/* Python function to get the breakpoint's number. */
528static PyObject *
529bppy_get_number (PyObject *self, void *closure)
530{
531 breakpoint_object *self_bp = (breakpoint_object *) self;
532
533 BPPY_REQUIRE_VALID (self_bp);
534
535 return PyInt_FromLong (self_bp->number);
536}
537
538/* Python function to get the breakpoint's thread ID. */
539static PyObject *
540bppy_get_thread (PyObject *self, void *closure)
541{
542 breakpoint_object *self_bp = (breakpoint_object *) self;
543
544 BPPY_REQUIRE_VALID (self_bp);
545
546 if (self_bp->bp->thread == -1)
547 Py_RETURN_NONE;
548
549 return PyInt_FromLong (self_bp->bp->thread);
550}
551
552/* Python function to get the breakpoint's task ID (in Ada). */
553static PyObject *
554bppy_get_task (PyObject *self, void *closure)
555{
556 breakpoint_object *self_bp = (breakpoint_object *) self;
557
558 BPPY_REQUIRE_VALID (self_bp);
559
560 if (self_bp->bp->task == 0)
561 Py_RETURN_NONE;
562
563 return PyInt_FromLong (self_bp->bp->task);
564}
565
566/* Python function to get the breakpoint's hit count. */
567static PyObject *
568bppy_get_hit_count (PyObject *self, void *closure)
569{
570 breakpoint_object *self_bp = (breakpoint_object *) self;
571
572 BPPY_REQUIRE_VALID (self_bp);
573
574 return PyInt_FromLong (self_bp->bp->hit_count);
575}
576
577/* Python function to get the breakpoint's ignore count. */
578static PyObject *
579bppy_get_ignore_count (PyObject *self, void *closure)
580{
581 breakpoint_object *self_bp = (breakpoint_object *) self;
582
583 BPPY_REQUIRE_VALID (self_bp);
584
585 return PyInt_FromLong (self_bp->bp->ignore_count);
586}
587
588/* Python function to create a new breakpoint. */
589static int
590bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
591{
592 static char *keywords[] = { "spec", "type", "wp_class", "internal", NULL };
a45ae5f8 593 const char *spec;
c50c785c
JM
594 int type = bp_breakpoint;
595 int access_type = hw_write;
596 PyObject *internal = NULL;
597 int internal_bp = 0;
598 volatile struct gdb_exception except;
599
600 if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiO", keywords,
601 &spec, &type, &access_type, &internal))
602 return -1;
603
604 if (internal)
605 {
606 internal_bp = PyObject_IsTrue (internal);
607 if (internal_bp == -1)
608 return -1;
609 }
610
611 bppy_pending_object = (breakpoint_object *) self;
612 bppy_pending_object->number = -1;
613 bppy_pending_object->bp = NULL;
614
615 TRY_CATCH (except, RETURN_MASK_ALL)
616 {
a45ae5f8
JM
617 char *copy = xstrdup (spec);
618 struct cleanup *cleanup = make_cleanup (xfree, copy);
619
c50c785c
JM
620 switch (type)
621 {
622 case bp_breakpoint:
623 {
624 create_breakpoint (python_gdbarch,
ef5ccd6c 625 copy, NULL, -1, NULL,
c50c785c
JM
626 0,
627 0, bp_breakpoint,
628 0,
629 AUTO_BOOLEAN_TRUE,
a45ae5f8
JM
630 &bkpt_breakpoint_ops,
631 0, 1, internal_bp, 0);
c50c785c
JM
632 break;
633 }
634 case bp_watchpoint:
635 {
636 if (access_type == hw_write)
a45ae5f8 637 watch_command_wrapper (copy, 0, internal_bp);
c50c785c 638 else if (access_type == hw_access)
a45ae5f8 639 awatch_command_wrapper (copy, 0, internal_bp);
c50c785c 640 else if (access_type == hw_read)
a45ae5f8 641 rwatch_command_wrapper (copy, 0, internal_bp);
c50c785c
JM
642 else
643 error(_("Cannot understand watchpoint access type."));
644 break;
645 }
646 default:
647 error(_("Do not understand breakpoint type to set."));
648 }
a45ae5f8
JM
649
650 do_cleanups (cleanup);
c50c785c
JM
651 }
652 if (except.reason < 0)
653 {
654 PyErr_Format (except.reason == RETURN_QUIT
655 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
656 "%s", except.message);
657 return -1;
658 }
659
660 BPPY_SET_REQUIRE_VALID ((breakpoint_object *) self);
661 return 0;
662}
663
664\f
665
666static int
667build_bp_list (struct breakpoint *b, void *arg)
668{
669 PyObject *list = arg;
670 PyObject *bp = (PyObject *) b->py_bp_object;
671 int iserr = 0;
672
673 /* Not all breakpoints will have a companion Python object.
674 Only breakpoints that were created via bppy_new, or
675 breakpoints that were created externally and are tracked by
676 the Python Scripting API. */
677 if (bp)
678 iserr = PyList_Append (list, bp);
679
680 if (iserr == -1)
681 return 1;
682
683 return 0;
684}
685
686/* Static function to return a tuple holding all breakpoints. */
687
688PyObject *
689gdbpy_breakpoints (PyObject *self, PyObject *args)
690{
a45ae5f8 691 PyObject *list, *tuple;
c50c785c
JM
692
693 if (bppy_live == 0)
694 Py_RETURN_NONE;
695
696 list = PyList_New (0);
697 if (!list)
698 return NULL;
699
700 /* If iteratre_over_breakpoints returns non NULL it signals an error
701 condition. In that case abandon building the list and return
702 NULL. */
703 if (iterate_over_breakpoints (build_bp_list, list) != NULL)
704 {
705 Py_DECREF (list);
706 return NULL;
707 }
708
a45ae5f8
JM
709 tuple = PyList_AsTuple (list);
710 Py_DECREF (list);
711
712 return tuple;
c50c785c
JM
713}
714
715/* Call the "stop" method (if implemented) in the breakpoint
716 class. If the method returns True, the inferior will be
717 stopped at the breakpoint. Otherwise the inferior will be
718 allowed to continue. */
719
720int
721gdbpy_should_stop (struct breakpoint_object *bp_obj)
722{
723 int stop = 1;
724
725 PyObject *py_bp = (PyObject *) bp_obj;
726 struct breakpoint *b = bp_obj->bp;
727 struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
728 struct cleanup *cleanup = ensure_python_env (garch, current_language);
729
a45ae5f8
JM
730 if (bp_obj->is_finish_bp)
731 bpfinishpy_pre_stop_hook (bp_obj);
732
c50c785c
JM
733 if (PyObject_HasAttrString (py_bp, stop_func))
734 {
735 PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);
736
737 if (result)
738 {
739 int evaluate = PyObject_IsTrue (result);
740
741 if (evaluate == -1)
742 gdbpy_print_stack ();
743
744 /* If the "stop" function returns False that means
745 the Python breakpoint wants GDB to continue. */
746 if (! evaluate)
747 stop = 0;
748
749 Py_DECREF (result);
750 }
751 else
752 gdbpy_print_stack ();
753 }
a45ae5f8
JM
754
755 if (bp_obj->is_finish_bp)
756 bpfinishpy_post_stop_hook (bp_obj);
757
c50c785c
JM
758 do_cleanups (cleanup);
759
760 return stop;
761}
762
763/* Checks if the "stop" method exists in this breakpoint.
764 Used by condition_command to ensure mutual exclusion of breakpoint
765 conditions. */
766
767int
768gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
769{
770 int has_func = 0;
771 PyObject *py_bp = (PyObject *) bp_obj;
772 struct gdbarch *garch = bp_obj->bp->gdbarch ? bp_obj->bp->gdbarch :
773 get_current_arch ();
774 struct cleanup *cleanup = ensure_python_env (garch, current_language);
775
a45ae5f8
JM
776 if (py_bp != NULL)
777 has_func = PyObject_HasAttrString (py_bp, stop_func);
c50c785c 778
c50c785c
JM
779 do_cleanups (cleanup);
780
781 return has_func;
782}
783
784\f
785
786/* Event callback functions. */
787
788/* Callback that is used when a breakpoint is created. This function
789 will create a new Python breakpoint object. */
790static void
a45ae5f8 791gdbpy_breakpoint_created (struct breakpoint *bp)
c50c785c
JM
792{
793 breakpoint_object *newbp;
c50c785c
JM
794 PyGILState_STATE state;
795
a45ae5f8 796 if (bp->number < 0 && bppy_pending_object == NULL)
c50c785c
JM
797 return;
798
799 if (bp->type != bp_breakpoint
800 && bp->type != bp_watchpoint
801 && bp->type != bp_hardware_watchpoint
802 && bp->type != bp_read_watchpoint
803 && bp->type != bp_access_watchpoint)
804 return;
805
806 state = PyGILState_Ensure ();
807
808 if (bppy_pending_object)
809 {
810 newbp = bppy_pending_object;
811 bppy_pending_object = NULL;
812 }
813 else
814 newbp = PyObject_New (breakpoint_object, &breakpoint_object_type);
815 if (newbp)
816 {
a45ae5f8 817 newbp->number = bp->number;
c50c785c
JM
818 newbp->bp = bp;
819 newbp->bp->py_bp_object = newbp;
a45ae5f8 820 newbp->is_finish_bp = 0;
c50c785c
JM
821 Py_INCREF (newbp);
822 ++bppy_live;
823 }
824 else
825 {
826 PyErr_SetString (PyExc_RuntimeError,
827 _("Error while creating breakpoint from GDB."));
828 gdbpy_print_stack ();
829 }
830
831 PyGILState_Release (state);
832}
833
834/* Callback that is used when a breakpoint is deleted. This will
835 invalidate the corresponding Python object. */
836static void
a45ae5f8 837gdbpy_breakpoint_deleted (struct breakpoint *b)
c50c785c 838{
a45ae5f8 839 int num = b->number;
c50c785c
JM
840 PyGILState_STATE state;
841 struct breakpoint *bp = NULL;
842 breakpoint_object *bp_obj;
843
844 state = PyGILState_Ensure ();
845 bp = get_breakpoint (num);
a45ae5f8 846 if (bp)
c50c785c 847 {
a45ae5f8
JM
848 bp_obj = bp->py_bp_object;
849 if (bp_obj)
850 {
851 bp_obj->bp = NULL;
852 --bppy_live;
853 Py_DECREF (bp_obj);
854 }
c50c785c
JM
855 }
856 PyGILState_Release (state);
857}
858
859\f
860
861/* Initialize the Python breakpoint code. */
862void
863gdbpy_initialize_breakpoints (void)
864{
865 int i;
866
a45ae5f8 867 breakpoint_object_type.tp_new = PyType_GenericNew;
c50c785c
JM
868 if (PyType_Ready (&breakpoint_object_type) < 0)
869 return;
870
871 Py_INCREF (&breakpoint_object_type);
872 PyModule_AddObject (gdb_module, "Breakpoint",
873 (PyObject *) &breakpoint_object_type);
874
875 observer_attach_breakpoint_created (gdbpy_breakpoint_created);
876 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
877
878 /* Add breakpoint types constants. */
879 for (i = 0; pybp_codes[i].name; ++i)
880 {
881 if (PyModule_AddIntConstant (gdb_module,
882 /* Cast needed for Python 2.4. */
883 (char *) pybp_codes[i].name,
884 pybp_codes[i].code) < 0)
885 return;
886 }
887
888 /* Add watchpoint types constants. */
889 for (i = 0; pybp_watch_types[i].name; ++i)
890 {
891 if (PyModule_AddIntConstant (gdb_module,
892 /* Cast needed for Python 2.4. */
893 (char *) pybp_watch_types[i].name,
894 pybp_watch_types[i].code) < 0)
895 return;
896 }
897
898}
899
900\f
901
902/* Helper function that overrides this Python object's
903 PyObject_GenericSetAttr to allow extra validation of the attribute
904 being set. */
905
906static int
907local_setattro (PyObject *self, PyObject *name, PyObject *v)
908{
909 breakpoint_object *obj = (breakpoint_object *) self;
910 char *attr = python_string_to_host_string (name);
911
912 if (attr == NULL)
913 return -1;
914
915 /* If the attribute trying to be set is the "stop" method,
916 but we already have a condition set in the CLI, disallow this
917 operation. */
918 if (strcmp (attr, stop_func) == 0 && obj->bp->cond_string)
919 {
920 xfree (attr);
921 PyErr_SetString (PyExc_RuntimeError,
922 _("Cannot set 'stop' method. There is an " \
923 "existing GDB condition attached to the " \
924 "breakpoint."));
925 return -1;
926 }
927
928 xfree (attr);
929
930 return PyObject_GenericSetAttr ((PyObject *)self, name, v);
931}
932
933static PyGetSetDef breakpoint_object_getset[] = {
934 { "enabled", bppy_get_enabled, bppy_set_enabled,
935 "Boolean telling whether the breakpoint is enabled.", NULL },
936 { "silent", bppy_get_silent, bppy_set_silent,
937 "Boolean telling whether the breakpoint is silent.", NULL },
938 { "thread", bppy_get_thread, bppy_set_thread,
939 "Thread ID for the breakpoint.\n\
940If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
941If the value is None, then this breakpoint is not thread-specific.\n\
942No other type of value can be used.", NULL },
943 { "task", bppy_get_task, bppy_set_task,
944 "Thread ID for the breakpoint.\n\
945If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
946If the value is None, then this breakpoint is not task-specific.\n\
947No other type of value can be used.", NULL },
948 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
949 "Number of times this breakpoint should be automatically continued.",
950 NULL },
951 { "number", bppy_get_number, NULL,
952 "Breakpoint's number assigned by GDB.", NULL },
953 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
954 "Number of times the breakpoint has been hit.\n\
955Can be set to zero to clear the count. No other value is valid\n\
956when setting this property.", NULL },
957 { "location", bppy_get_location, NULL,
958 "Location of the breakpoint, as specified by the user.", NULL},
959 { "expression", bppy_get_expression, NULL,
960 "Expression of the breakpoint, as specified by the user.", NULL},
961 { "condition", bppy_get_condition, bppy_set_condition,
962 "Condition of the breakpoint, as specified by the user,\
963or None if no condition set."},
964 { "commands", bppy_get_commands, NULL,
965 "Commands of the breakpoint, as specified by the user."},
966 { "type", bppy_get_type, NULL,
967 "Type of breakpoint."},
968 { "visible", bppy_get_visibility, NULL,
969 "Whether the breakpoint is visible to the user."},
970 { NULL } /* Sentinel. */
971};
972
973static PyMethodDef breakpoint_object_methods[] =
974{
975 { "is_valid", bppy_is_valid, METH_NOARGS,
976 "Return true if this breakpoint is valid, false if not." },
977 { "delete", bppy_delete_breakpoint, METH_NOARGS,
978 "Delete the underlying GDB breakpoint." },
979 { NULL } /* Sentinel. */
980};
981
a45ae5f8 982PyTypeObject breakpoint_object_type =
c50c785c 983{
ef5ccd6c 984 PyVarObject_HEAD_INIT (NULL, 0)
c50c785c
JM
985 "gdb.Breakpoint", /*tp_name*/
986 sizeof (breakpoint_object), /*tp_basicsize*/
987 0, /*tp_itemsize*/
988 0, /*tp_dealloc*/
989 0, /*tp_print*/
990 0, /*tp_getattr*/
991 0, /*tp_setattr*/
992 0, /*tp_compare*/
993 0, /*tp_repr*/
994 0, /*tp_as_number*/
995 0, /*tp_as_sequence*/
996 0, /*tp_as_mapping*/
997 0, /*tp_hash */
998 0, /*tp_call*/
999 0, /*tp_str*/
1000 0, /*tp_getattro*/
1001 (setattrofunc)local_setattro, /*tp_setattro */
1002 0, /*tp_as_buffer*/
1003 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1004 "GDB breakpoint object", /* tp_doc */
1005 0, /* tp_traverse */
1006 0, /* tp_clear */
1007 0, /* tp_richcompare */
1008 0, /* tp_weaklistoffset */
1009 0, /* tp_iter */
1010 0, /* tp_iternext */
1011 breakpoint_object_methods, /* tp_methods */
1012 0, /* tp_members */
1013 breakpoint_object_getset, /* tp_getset */
1014 0, /* tp_base */
1015 0, /* tp_dict */
1016 0, /* tp_descr_get */
1017 0, /* tp_descr_set */
1018 0, /* tp_dictoffset */
1019 bppy_init, /* tp_init */
1020 0, /* tp_alloc */
c50c785c 1021};