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