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