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