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