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