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