Allow breakpoint commands to be set from Python
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
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 "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 /* 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. */
42 gdbpy_breakpoint_object *bppy_pending_object;
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 {
81 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
92 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
106 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
118 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
150 GDB_PY_SET_HANDLE_EXCEPTION (except);
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 {
161 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
192 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
233 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
256 GDB_PY_SET_HANDLE_EXCEPTION (except);
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 {
289 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
311 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
341 GDB_PY_SET_HANDLE_EXCEPTION (except);
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 {
352 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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;
387 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
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;
405 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
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;
427 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
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 {
444 gdb::unique_xmalloc_ptr<char> exp_holder;
445 const char *exp = NULL;
446 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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
477 GDB_PY_SET_HANDLE_EXCEPTION (except);
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 {
486 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
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 {
499 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
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 /* Set the commands attached to a breakpoint. Returns 0 on success.
514 Returns -1 on error, with a python exception set. */
515 static int
516 bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
517 {
518 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
519 struct breakpoint *bp = self_bp->bp;
520 struct gdb_exception except = exception_none;
521
522 BPPY_SET_REQUIRE_VALID (self_bp);
523
524 gdb::unique_xmalloc_ptr<char> commands
525 (python_string_to_host_string (newvalue));
526 if (commands == nullptr)
527 return -1;
528
529 TRY
530 {
531 bool first = true;
532 char *save_ptr = nullptr;
533 auto reader
534 = [&] ()
535 {
536 const char *result = strtok_r (first ? commands.get () : nullptr,
537 "\n", &save_ptr);
538 first = false;
539 return result;
540 };
541
542 counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
543 breakpoint_set_commands (self_bp->bp, std::move (lines));
544 }
545 CATCH (ex, RETURN_MASK_ALL)
546 {
547 except = ex;
548 }
549 END_CATCH
550
551 GDB_PY_SET_HANDLE_EXCEPTION (except);
552
553 return 0;
554 }
555
556 /* Python function to get the breakpoint type. */
557 static PyObject *
558 bppy_get_type (PyObject *self, void *closure)
559 {
560 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
561
562 BPPY_REQUIRE_VALID (self_bp);
563
564 return PyInt_FromLong (self_bp->bp->type);
565 }
566
567 /* Python function to get the visibility of the breakpoint. */
568
569 static PyObject *
570 bppy_get_visibility (PyObject *self, void *closure)
571 {
572 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
573
574 BPPY_REQUIRE_VALID (self_bp);
575
576 if (user_breakpoint_p (self_bp->bp))
577 Py_RETURN_TRUE;
578
579 Py_RETURN_FALSE;
580 }
581
582 /* Python function to determine if the breakpoint is a temporary
583 breakpoint. */
584
585 static PyObject *
586 bppy_get_temporary (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 (self_bp->bp->disposition == disp_del
593 || self_bp->bp->disposition == disp_del_at_next_stop)
594 Py_RETURN_TRUE;
595
596 Py_RETURN_FALSE;
597 }
598
599 /* Python function to determine if the breakpoint is a pending
600 breakpoint. */
601
602 static PyObject *
603 bppy_get_pending (PyObject *self, void *closure)
604 {
605 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
606
607 BPPY_REQUIRE_VALID (self_bp);
608
609 if (is_watchpoint (self_bp->bp))
610 Py_RETURN_FALSE;
611 if (pending_breakpoint_p (self_bp->bp))
612 Py_RETURN_TRUE;
613
614 Py_RETURN_FALSE;
615 }
616
617 /* Python function to get the breakpoint's number. */
618 static PyObject *
619 bppy_get_number (PyObject *self, void *closure)
620 {
621 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
622
623 BPPY_REQUIRE_VALID (self_bp);
624
625 return PyInt_FromLong (self_bp->number);
626 }
627
628 /* Python function to get the breakpoint's thread ID. */
629 static PyObject *
630 bppy_get_thread (PyObject *self, void *closure)
631 {
632 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
633
634 BPPY_REQUIRE_VALID (self_bp);
635
636 if (self_bp->bp->thread == -1)
637 Py_RETURN_NONE;
638
639 return PyInt_FromLong (self_bp->bp->thread);
640 }
641
642 /* Python function to get the breakpoint's task ID (in Ada). */
643 static PyObject *
644 bppy_get_task (PyObject *self, void *closure)
645 {
646 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
647
648 BPPY_REQUIRE_VALID (self_bp);
649
650 if (self_bp->bp->task == 0)
651 Py_RETURN_NONE;
652
653 return PyInt_FromLong (self_bp->bp->task);
654 }
655
656 /* Python function to get the breakpoint's hit count. */
657 static PyObject *
658 bppy_get_hit_count (PyObject *self, void *closure)
659 {
660 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
661
662 BPPY_REQUIRE_VALID (self_bp);
663
664 return PyInt_FromLong (self_bp->bp->hit_count);
665 }
666
667 /* Python function to get the breakpoint's ignore count. */
668 static PyObject *
669 bppy_get_ignore_count (PyObject *self, void *closure)
670 {
671 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
672
673 BPPY_REQUIRE_VALID (self_bp);
674
675 return PyInt_FromLong (self_bp->bp->ignore_count);
676 }
677
678 /* Internal function to validate the Python parameters/keywords
679 provided to bppy_init. */
680
681 static int
682 bppy_init_validate_args (const char *spec, char *source,
683 char *function, char *label,
684 char *line, enum bptype type)
685 {
686 /* If spec is defined, ensure that none of the explicit location
687 keywords are also defined. */
688 if (spec != NULL)
689 {
690 if (source != NULL || function != NULL || label != NULL || line != NULL)
691 {
692 PyErr_SetString (PyExc_RuntimeError,
693 _("Breakpoints specified with spec cannot "
694 "have source, function, label or line defined."));
695 return -1;
696 }
697 }
698 else
699 {
700 /* If spec isn't defined, ensure that the user is not trying to
701 define a watchpoint with an explicit location. */
702 if (type == bp_watchpoint)
703 {
704 PyErr_SetString (PyExc_RuntimeError,
705 _("Watchpoints cannot be set by explicit "
706 "location parameters."));
707 return -1;
708 }
709 else
710 {
711 /* Otherwise, ensure some explicit locations are defined. */
712 if (source == NULL && function == NULL && label == NULL
713 && line == NULL)
714 {
715 PyErr_SetString (PyExc_RuntimeError,
716 _("Neither spec nor explicit location set."));
717 return -1;
718 }
719 /* Finally, if source is specified, ensure that line, label
720 or function are specified too. */
721 if (source != NULL && function == NULL && label == NULL
722 && line == NULL)
723 {
724 PyErr_SetString (PyExc_RuntimeError,
725 _("Specifying a source must also include a "
726 "line, label or function."));
727 return -1;
728 }
729 }
730 }
731 return 1;
732 }
733
734 /* Python function to create a new breakpoint. */
735 static int
736 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
737 {
738 static const char *keywords[] = { "spec", "type", "wp_class", "internal",
739 "temporary","source", "function",
740 "label", "line", "qualified", NULL };
741 const char *spec = NULL;
742 enum bptype type = bp_breakpoint;
743 int access_type = hw_write;
744 PyObject *internal = NULL;
745 PyObject *temporary = NULL;
746 PyObject *lineobj = NULL;;
747 int internal_bp = 0;
748 int temporary_bp = 0;
749 gdb::unique_xmalloc_ptr<char> line;
750 char *label = NULL;
751 char *source = NULL;
752 char *function = NULL;
753 PyObject * qualified = NULL;
754
755 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
756 &spec, &type, &access_type,
757 &internal,
758 &temporary, &source,
759 &function, &label, &lineobj,
760 &qualified))
761 return -1;
762
763
764 if (lineobj != NULL)
765 {
766 if (PyInt_Check (lineobj))
767 line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
768 else if (PyString_Check (lineobj))
769 line = python_string_to_host_string (lineobj);
770 else
771 {
772 PyErr_SetString (PyExc_RuntimeError,
773 _("Line keyword should be an integer or a string. "));
774 return -1;
775 }
776 }
777
778 if (internal)
779 {
780 internal_bp = PyObject_IsTrue (internal);
781 if (internal_bp == -1)
782 return -1;
783 }
784
785 if (temporary != NULL)
786 {
787 temporary_bp = PyObject_IsTrue (temporary);
788 if (temporary_bp == -1)
789 return -1;
790 }
791
792 if (bppy_init_validate_args (spec, source, function, label, line.get (),
793 type) == -1)
794 return -1;
795
796 bppy_pending_object = (gdbpy_breakpoint_object *) self;
797 bppy_pending_object->number = -1;
798 bppy_pending_object->bp = NULL;
799
800 TRY
801 {
802 switch (type)
803 {
804 case bp_breakpoint:
805 {
806 event_location_up location;
807 symbol_name_match_type func_name_match_type
808 = (qualified != NULL && PyObject_IsTrue (qualified)
809 ? symbol_name_match_type::FULL
810 : symbol_name_match_type::WILD);
811
812 if (spec != NULL)
813 {
814 gdb::unique_xmalloc_ptr<char>
815 copy_holder (xstrdup (skip_spaces (spec)));
816 const char *copy = copy_holder.get ();
817
818 location = string_to_event_location (&copy,
819 current_language,
820 func_name_match_type);
821 }
822 else
823 {
824 struct explicit_location explicit_loc;
825
826 initialize_explicit_location (&explicit_loc);
827 explicit_loc.source_filename = source;
828 explicit_loc.function_name = function;
829 explicit_loc.label_name = label;
830
831 if (line != NULL)
832 explicit_loc.line_offset =
833 linespec_parse_line_offset (line.get ());
834
835 explicit_loc.func_name_match_type = func_name_match_type;
836
837 location = new_explicit_location (&explicit_loc);
838 }
839
840 create_breakpoint (python_gdbarch,
841 location.get (), NULL, -1, NULL,
842 0,
843 temporary_bp, bp_breakpoint,
844 0,
845 AUTO_BOOLEAN_TRUE,
846 &bkpt_breakpoint_ops,
847 0, 1, internal_bp, 0);
848 break;
849 }
850 case bp_watchpoint:
851 {
852 gdb::unique_xmalloc_ptr<char>
853 copy_holder (xstrdup (skip_spaces (spec)));
854 char *copy = copy_holder.get ();
855
856 if (access_type == hw_write)
857 watch_command_wrapper (copy, 0, internal_bp);
858 else if (access_type == hw_access)
859 awatch_command_wrapper (copy, 0, internal_bp);
860 else if (access_type == hw_read)
861 rwatch_command_wrapper (copy, 0, internal_bp);
862 else
863 error(_("Cannot understand watchpoint access type."));
864 break;
865 }
866 default:
867 error(_("Do not understand breakpoint type to set."));
868 }
869 }
870 CATCH (except, RETURN_MASK_ALL)
871 {
872 bppy_pending_object = NULL;
873 PyErr_Format (except.reason == RETURN_QUIT
874 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
875 "%s", except.message);
876 return -1;
877 }
878 END_CATCH
879
880 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
881 return 0;
882 }
883
884 \f
885
886 static int
887 build_bp_list (struct breakpoint *b, void *arg)
888 {
889 PyObject *list = (PyObject *) arg;
890 PyObject *bp = (PyObject *) b->py_bp_object;
891 int iserr = 0;
892
893 /* Not all breakpoints will have a companion Python object.
894 Only breakpoints that were created via bppy_new, or
895 breakpoints that were created externally and are tracked by
896 the Python Scripting API. */
897 if (bp)
898 iserr = PyList_Append (list, bp);
899
900 if (iserr == -1)
901 return 1;
902
903 return 0;
904 }
905
906 /* Static function to return a tuple holding all breakpoints. */
907
908 PyObject *
909 gdbpy_breakpoints (PyObject *self, PyObject *args)
910 {
911 if (bppy_live == 0)
912 return PyTuple_New (0);
913
914 gdbpy_ref<> list (PyList_New (0));
915 if (list == NULL)
916 return NULL;
917
918 /* If iterate_over_breakpoints returns non NULL it signals an error
919 condition. In that case abandon building the list and return
920 NULL. */
921 if (iterate_over_breakpoints (build_bp_list, list.get ()) != NULL)
922 return NULL;
923
924 return PyList_AsTuple (list.get ());
925 }
926
927 /* Call the "stop" method (if implemented) in the breakpoint
928 class. If the method returns True, the inferior will be
929 stopped at the breakpoint. Otherwise the inferior will be
930 allowed to continue. */
931
932 enum ext_lang_bp_stop
933 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
934 struct breakpoint *b)
935 {
936 int stop;
937 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
938 PyObject *py_bp = (PyObject *) bp_obj;
939 struct gdbarch *garch;
940
941 if (bp_obj == NULL)
942 return EXT_LANG_BP_STOP_UNSET;
943
944 stop = -1;
945 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
946
947 gdbpy_enter enter_py (garch, current_language);
948
949 if (bp_obj->is_finish_bp)
950 bpfinishpy_pre_stop_hook (bp_obj);
951
952 if (PyObject_HasAttrString (py_bp, stop_func))
953 {
954 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
955
956 stop = 1;
957 if (result != NULL)
958 {
959 int evaluate = PyObject_IsTrue (result.get ());
960
961 if (evaluate == -1)
962 gdbpy_print_stack ();
963
964 /* If the "stop" function returns False that means
965 the Python breakpoint wants GDB to continue. */
966 if (! evaluate)
967 stop = 0;
968 }
969 else
970 gdbpy_print_stack ();
971 }
972
973 if (bp_obj->is_finish_bp)
974 bpfinishpy_post_stop_hook (bp_obj);
975
976 if (stop < 0)
977 return EXT_LANG_BP_STOP_UNSET;
978 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
979 }
980
981 /* Checks if the "stop" method exists in this breakpoint.
982 Used by condition_command to ensure mutual exclusion of breakpoint
983 conditions. */
984
985 int
986 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
987 struct breakpoint *b)
988 {
989 PyObject *py_bp;
990 struct gdbarch *garch;
991
992 if (b->py_bp_object == NULL)
993 return 0;
994
995 py_bp = (PyObject *) b->py_bp_object;
996 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
997
998 gdbpy_enter enter_py (garch, current_language);
999 return PyObject_HasAttrString (py_bp, stop_func);
1000 }
1001
1002 \f
1003
1004 /* Event callback functions. */
1005
1006 /* Callback that is used when a breakpoint is created. This function
1007 will create a new Python breakpoint object. */
1008 static void
1009 gdbpy_breakpoint_created (struct breakpoint *bp)
1010 {
1011 gdbpy_breakpoint_object *newbp;
1012 PyGILState_STATE state;
1013
1014 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
1015 return;
1016
1017 if (bp->type != bp_breakpoint
1018 && bp->type != bp_watchpoint
1019 && bp->type != bp_hardware_watchpoint
1020 && bp->type != bp_read_watchpoint
1021 && bp->type != bp_access_watchpoint)
1022 return;
1023
1024 state = PyGILState_Ensure ();
1025
1026 if (bppy_pending_object)
1027 {
1028 newbp = bppy_pending_object;
1029 bppy_pending_object = NULL;
1030 }
1031 else
1032 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
1033 if (newbp)
1034 {
1035 newbp->number = bp->number;
1036 newbp->bp = bp;
1037 newbp->bp->py_bp_object = newbp;
1038 newbp->is_finish_bp = 0;
1039 Py_INCREF (newbp);
1040 ++bppy_live;
1041 }
1042 else
1043 {
1044 PyErr_SetString (PyExc_RuntimeError,
1045 _("Error while creating breakpoint from GDB."));
1046 gdbpy_print_stack ();
1047 }
1048
1049 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1050 {
1051 if (evpy_emit_event ((PyObject *) newbp,
1052 gdb_py_events.breakpoint_created) < 0)
1053 gdbpy_print_stack ();
1054 }
1055
1056 PyGILState_Release (state);
1057 }
1058
1059 /* Callback that is used when a breakpoint is deleted. This will
1060 invalidate the corresponding Python object. */
1061 static void
1062 gdbpy_breakpoint_deleted (struct breakpoint *b)
1063 {
1064 int num = b->number;
1065 PyGILState_STATE state;
1066 struct breakpoint *bp = NULL;
1067
1068 state = PyGILState_Ensure ();
1069 bp = get_breakpoint (num);
1070 if (bp)
1071 {
1072 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1073 if (bp_obj != NULL)
1074 {
1075 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1076 {
1077 if (evpy_emit_event ((PyObject *) bp_obj.get (),
1078 gdb_py_events.breakpoint_deleted) < 0)
1079 gdbpy_print_stack ();
1080 }
1081
1082 bp_obj->bp = NULL;
1083 --bppy_live;
1084 }
1085 }
1086 PyGILState_Release (state);
1087 }
1088
1089 /* Callback that is used when a breakpoint is modified. */
1090
1091 static void
1092 gdbpy_breakpoint_modified (struct breakpoint *b)
1093 {
1094 int num = b->number;
1095 PyGILState_STATE state;
1096 struct breakpoint *bp = NULL;
1097
1098 state = PyGILState_Ensure ();
1099 bp = get_breakpoint (num);
1100 if (bp)
1101 {
1102 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1103 if (bp_obj)
1104 {
1105 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1106 {
1107 if (evpy_emit_event (bp_obj,
1108 gdb_py_events.breakpoint_modified) < 0)
1109 gdbpy_print_stack ();
1110 }
1111 }
1112 }
1113 PyGILState_Release (state);
1114 }
1115
1116 \f
1117
1118 /* Initialize the Python breakpoint code. */
1119 int
1120 gdbpy_initialize_breakpoints (void)
1121 {
1122 int i;
1123
1124 breakpoint_object_type.tp_new = PyType_GenericNew;
1125 if (PyType_Ready (&breakpoint_object_type) < 0)
1126 return -1;
1127
1128 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1129 (PyObject *) &breakpoint_object_type) < 0)
1130 return -1;
1131
1132 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created);
1133 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted);
1134 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified);
1135
1136 /* Add breakpoint types constants. */
1137 for (i = 0; pybp_codes[i].name; ++i)
1138 {
1139 if (PyModule_AddIntConstant (gdb_module,
1140 /* Cast needed for Python 2.4. */
1141 (char *) pybp_codes[i].name,
1142 pybp_codes[i].code) < 0)
1143 return -1;
1144 }
1145
1146 /* Add watchpoint types constants. */
1147 for (i = 0; pybp_watch_types[i].name; ++i)
1148 {
1149 if (PyModule_AddIntConstant (gdb_module,
1150 /* Cast needed for Python 2.4. */
1151 (char *) pybp_watch_types[i].name,
1152 pybp_watch_types[i].code) < 0)
1153 return -1;
1154 }
1155
1156 return 0;
1157 }
1158
1159 \f
1160
1161 /* Helper function that overrides this Python object's
1162 PyObject_GenericSetAttr to allow extra validation of the attribute
1163 being set. */
1164
1165 static int
1166 local_setattro (PyObject *self, PyObject *name, PyObject *v)
1167 {
1168 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
1169 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
1170
1171 if (attr == NULL)
1172 return -1;
1173
1174 /* If the attribute trying to be set is the "stop" method,
1175 but we already have a condition set in the CLI or other extension
1176 language, disallow this operation. */
1177 if (strcmp (attr.get (), stop_func) == 0)
1178 {
1179 const struct extension_language_defn *extlang = NULL;
1180
1181 if (obj->bp->cond_string != NULL)
1182 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1183 if (extlang == NULL)
1184 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1185 if (extlang != NULL)
1186 {
1187 std::string error_text
1188 = string_printf (_("Only one stop condition allowed. There is"
1189 " currently a %s stop condition defined for"
1190 " this breakpoint."),
1191 ext_lang_capitalized_name (extlang));
1192 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
1193 return -1;
1194 }
1195 }
1196
1197 return PyObject_GenericSetAttr ((PyObject *)self, name, v);
1198 }
1199
1200 static gdb_PyGetSetDef breakpoint_object_getset[] = {
1201 { "enabled", bppy_get_enabled, bppy_set_enabled,
1202 "Boolean telling whether the breakpoint is enabled.", NULL },
1203 { "silent", bppy_get_silent, bppy_set_silent,
1204 "Boolean telling whether the breakpoint is silent.", NULL },
1205 { "thread", bppy_get_thread, bppy_set_thread,
1206 "Thread ID for the breakpoint.\n\
1207 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1208 If the value is None, then this breakpoint is not thread-specific.\n\
1209 No other type of value can be used.", NULL },
1210 { "task", bppy_get_task, bppy_set_task,
1211 "Thread ID for the breakpoint.\n\
1212 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1213 If the value is None, then this breakpoint is not task-specific.\n\
1214 No other type of value can be used.", NULL },
1215 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1216 "Number of times this breakpoint should be automatically continued.",
1217 NULL },
1218 { "number", bppy_get_number, NULL,
1219 "Breakpoint's number assigned by GDB.", NULL },
1220 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1221 "Number of times the breakpoint has been hit.\n\
1222 Can be set to zero to clear the count. No other value is valid\n\
1223 when setting this property.", NULL },
1224 { "location", bppy_get_location, NULL,
1225 "Location of the breakpoint, as specified by the user.", NULL},
1226 { "expression", bppy_get_expression, NULL,
1227 "Expression of the breakpoint, as specified by the user.", NULL},
1228 { "condition", bppy_get_condition, bppy_set_condition,
1229 "Condition of the breakpoint, as specified by the user,\
1230 or None if no condition set."},
1231 { "commands", bppy_get_commands, bppy_set_commands,
1232 "Commands of the breakpoint, as specified by the user."},
1233 { "type", bppy_get_type, NULL,
1234 "Type of breakpoint."},
1235 { "visible", bppy_get_visibility, NULL,
1236 "Whether the breakpoint is visible to the user."},
1237 { "temporary", bppy_get_temporary, NULL,
1238 "Whether this breakpoint is a temporary breakpoint."},
1239 { "pending", bppy_get_pending, NULL,
1240 "Whether this breakpoint is a pending breakpoint."},
1241 { NULL } /* Sentinel. */
1242 };
1243
1244 static PyMethodDef breakpoint_object_methods[] =
1245 {
1246 { "is_valid", bppy_is_valid, METH_NOARGS,
1247 "Return true if this breakpoint is valid, false if not." },
1248 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1249 "Delete the underlying GDB breakpoint." },
1250 { NULL } /* Sentinel. */
1251 };
1252
1253 PyTypeObject breakpoint_object_type =
1254 {
1255 PyVarObject_HEAD_INIT (NULL, 0)
1256 "gdb.Breakpoint", /*tp_name*/
1257 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
1258 0, /*tp_itemsize*/
1259 0, /*tp_dealloc*/
1260 0, /*tp_print*/
1261 0, /*tp_getattr*/
1262 0, /*tp_setattr*/
1263 0, /*tp_compare*/
1264 0, /*tp_repr*/
1265 0, /*tp_as_number*/
1266 0, /*tp_as_sequence*/
1267 0, /*tp_as_mapping*/
1268 0, /*tp_hash */
1269 0, /*tp_call*/
1270 0, /*tp_str*/
1271 0, /*tp_getattro*/
1272 (setattrofunc)local_setattro, /*tp_setattro */
1273 0, /*tp_as_buffer*/
1274 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1275 "GDB breakpoint object", /* tp_doc */
1276 0, /* tp_traverse */
1277 0, /* tp_clear */
1278 0, /* tp_richcompare */
1279 0, /* tp_weaklistoffset */
1280 0, /* tp_iter */
1281 0, /* tp_iternext */
1282 breakpoint_object_methods, /* tp_methods */
1283 0, /* tp_members */
1284 breakpoint_object_getset, /* tp_getset */
1285 0, /* tp_base */
1286 0, /* tp_dict */
1287 0, /* tp_descr_get */
1288 0, /* tp_descr_set */
1289 0, /* tp_dictoffset */
1290 bppy_init, /* tp_init */
1291 0, /* tp_alloc */
1292 };
This page took 0.057111 seconds and 5 git commands to generate.