* python/python.c (finish_python_initialization): Decref
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
CommitLineData
adc36818
PM
1/* Python interface to breakpoints
2
28e7fd62 3 Copyright (C) 2008-2013 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. */
e81b7af8 51 int code;
adc36818
PM
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);
b862ce75
TT
492 if (except.reason < 0)
493 {
494 do_cleanups (chain);
f3300387
TT
495 gdbpy_convert_exception (except);
496 return NULL;
b862ce75 497 }
adc36818 498
4c2d5724
MS
499 cmdstr = ui_file_xstrdup (string_file, &length);
500 make_cleanup (xfree, cmdstr);
adc36818
PM
501 result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL);
502 do_cleanups (chain);
adc36818
PM
503 return result;
504}
505
506/* Python function to get the breakpoint type. */
507static PyObject *
508bppy_get_type (PyObject *self, void *closure)
509{
510 breakpoint_object *self_bp = (breakpoint_object *) self;
511
512 BPPY_REQUIRE_VALID (self_bp);
513
514 return PyInt_FromLong (self_bp->bp->type);
515}
516
84f4c1fe
PM
517/* Python function to get the visibility of the breakpoint. */
518
519static PyObject *
520bppy_get_visibility (PyObject *self, void *closure)
521{
522 breakpoint_object *self_bp = (breakpoint_object *) self;
523
524 BPPY_REQUIRE_VALID (self_bp);
525
526 if (self_bp->bp->number < 0)
527 Py_RETURN_FALSE;
528
529 Py_RETURN_TRUE;
530}
531
adc36818
PM
532/* Python function to get the breakpoint's number. */
533static PyObject *
534bppy_get_number (PyObject *self, void *closure)
535{
536 breakpoint_object *self_bp = (breakpoint_object *) self;
537
538 BPPY_REQUIRE_VALID (self_bp);
539
540 return PyInt_FromLong (self_bp->number);
541}
542
543/* Python function to get the breakpoint's thread ID. */
544static PyObject *
545bppy_get_thread (PyObject *self, void *closure)
546{
547 breakpoint_object *self_bp = (breakpoint_object *) self;
548
549 BPPY_REQUIRE_VALID (self_bp);
550
551 if (self_bp->bp->thread == -1)
552 Py_RETURN_NONE;
553
554 return PyInt_FromLong (self_bp->bp->thread);
555}
556
557/* Python function to get the breakpoint's task ID (in Ada). */
558static PyObject *
559bppy_get_task (PyObject *self, void *closure)
560{
561 breakpoint_object *self_bp = (breakpoint_object *) self;
562
563 BPPY_REQUIRE_VALID (self_bp);
564
565 if (self_bp->bp->task == 0)
566 Py_RETURN_NONE;
567
568 return PyInt_FromLong (self_bp->bp->task);
569}
570
571/* Python function to get the breakpoint's hit count. */
572static PyObject *
573bppy_get_hit_count (PyObject *self, void *closure)
574{
575 breakpoint_object *self_bp = (breakpoint_object *) self;
576
577 BPPY_REQUIRE_VALID (self_bp);
578
579 return PyInt_FromLong (self_bp->bp->hit_count);
580}
581
582/* Python function to get the breakpoint's ignore count. */
583static PyObject *
584bppy_get_ignore_count (PyObject *self, void *closure)
585{
586 breakpoint_object *self_bp = (breakpoint_object *) self;
587
588 BPPY_REQUIRE_VALID (self_bp);
589
590 return PyInt_FromLong (self_bp->bp->ignore_count);
591}
592
593/* Python function to create a new breakpoint. */
7371cf6d
PM
594static int
595bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
adc36818 596{
84f4c1fe 597 static char *keywords[] = { "spec", "type", "wp_class", "internal", NULL };
ddd49eee 598 const char *spec;
adc36818
PM
599 int type = bp_breakpoint;
600 int access_type = hw_write;
84f4c1fe
PM
601 PyObject *internal = NULL;
602 int internal_bp = 0;
adc36818
PM
603 volatile struct gdb_exception except;
604
84f4c1fe
PM
605 if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiO", keywords,
606 &spec, &type, &access_type, &internal))
7371cf6d 607 return -1;
adc36818 608
84f4c1fe
PM
609 if (internal)
610 {
611 internal_bp = PyObject_IsTrue (internal);
612 if (internal_bp == -1)
7371cf6d 613 return -1;
84f4c1fe
PM
614 }
615
7371cf6d 616 bppy_pending_object = (breakpoint_object *) self;
adc36818
PM
617 bppy_pending_object->number = -1;
618 bppy_pending_object->bp = NULL;
619
620 TRY_CATCH (except, RETURN_MASK_ALL)
621 {
ddd49eee
TT
622 char *copy = xstrdup (spec);
623 struct cleanup *cleanup = make_cleanup (xfree, copy);
624
adc36818
PM
625 switch (type)
626 {
627 case bp_breakpoint:
628 {
629 create_breakpoint (python_gdbarch,
e7e0cddf 630 copy, NULL, -1, NULL,
adc36818 631 0,
0fb4aa4b 632 0, bp_breakpoint,
adc36818
PM
633 0,
634 AUTO_BOOLEAN_TRUE,
348d480f 635 &bkpt_breakpoint_ops,
44f238bb 636 0, 1, internal_bp, 0);
adc36818
PM
637 break;
638 }
639 case bp_watchpoint:
640 {
641 if (access_type == hw_write)
ddd49eee 642 watch_command_wrapper (copy, 0, internal_bp);
adc36818 643 else if (access_type == hw_access)
ddd49eee 644 awatch_command_wrapper (copy, 0, internal_bp);
adc36818 645 else if (access_type == hw_read)
ddd49eee 646 rwatch_command_wrapper (copy, 0, internal_bp);
adc36818
PM
647 else
648 error(_("Cannot understand watchpoint access type."));
649 break;
650 }
651 default:
652 error(_("Do not understand breakpoint type to set."));
653 }
ddd49eee
TT
654
655 do_cleanups (cleanup);
adc36818
PM
656 }
657 if (except.reason < 0)
658 {
7371cf6d
PM
659 PyErr_Format (except.reason == RETURN_QUIT
660 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
661 "%s", except.message);
662 return -1;
adc36818
PM
663 }
664
7371cf6d
PM
665 BPPY_SET_REQUIRE_VALID ((breakpoint_object *) self);
666 return 0;
adc36818
PM
667}
668
669\f
670
84f4c1fe
PM
671static int
672build_bp_list (struct breakpoint *b, void *arg)
673{
674 PyObject *list = arg;
50389644 675 PyObject *bp = (PyObject *) b->py_bp_object;
84f4c1fe
PM
676 int iserr = 0;
677
678 /* Not all breakpoints will have a companion Python object.
679 Only breakpoints that were created via bppy_new, or
680 breakpoints that were created externally and are tracked by
681 the Python Scripting API. */
682 if (bp)
683 iserr = PyList_Append (list, bp);
684
685 if (iserr == -1)
686 return 1;
687
688 return 0;
689}
690
adc36818
PM
691/* Static function to return a tuple holding all breakpoints. */
692
693PyObject *
694gdbpy_breakpoints (PyObject *self, PyObject *args)
695{
27ca1a5b 696 PyObject *list, *tuple;
adc36818
PM
697
698 if (bppy_live == 0)
699 Py_RETURN_NONE;
700
84f4c1fe
PM
701 list = PyList_New (0);
702 if (!list)
703 return NULL;
d59b6f6c 704
84f4c1fe
PM
705 /* If iteratre_over_breakpoints returns non NULL it signals an error
706 condition. In that case abandon building the list and return
707 NULL. */
708 if (iterate_over_breakpoints (build_bp_list, list) != NULL)
709 {
710 Py_DECREF (list);
711 return NULL;
adc36818 712 }
84f4c1fe 713
27ca1a5b
PM
714 tuple = PyList_AsTuple (list);
715 Py_DECREF (list);
716
717 return tuple;
adc36818
PM
718}
719
7371cf6d
PM
720/* Call the "stop" method (if implemented) in the breakpoint
721 class. If the method returns True, the inferior will be
722 stopped at the breakpoint. Otherwise the inferior will be
723 allowed to continue. */
724
725int
726gdbpy_should_stop (struct breakpoint_object *bp_obj)
727{
728 int stop = 1;
729
730 PyObject *py_bp = (PyObject *) bp_obj;
731 struct breakpoint *b = bp_obj->bp;
732 struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
733 struct cleanup *cleanup = ensure_python_env (garch, current_language);
734
cc72b2a2
KP
735 if (bp_obj->is_finish_bp)
736 bpfinishpy_pre_stop_hook (bp_obj);
737
7371cf6d
PM
738 if (PyObject_HasAttrString (py_bp, stop_func))
739 {
740 PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);
741
742 if (result)
743 {
744 int evaluate = PyObject_IsTrue (result);
745
746 if (evaluate == -1)
747 gdbpy_print_stack ();
748
749 /* If the "stop" function returns False that means
750 the Python breakpoint wants GDB to continue. */
751 if (! evaluate)
752 stop = 0;
753
754 Py_DECREF (result);
755 }
756 else
757 gdbpy_print_stack ();
758 }
cc72b2a2
KP
759
760 if (bp_obj->is_finish_bp)
761 bpfinishpy_post_stop_hook (bp_obj);
762
7371cf6d
PM
763 do_cleanups (cleanup);
764
765 return stop;
766}
767
768/* Checks if the "stop" method exists in this breakpoint.
769 Used by condition_command to ensure mutual exclusion of breakpoint
770 conditions. */
771
772int
773gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
774{
775 int has_func = 0;
776 PyObject *py_bp = (PyObject *) bp_obj;
777 struct gdbarch *garch = bp_obj->bp->gdbarch ? bp_obj->bp->gdbarch :
778 get_current_arch ();
779 struct cleanup *cleanup = ensure_python_env (garch, current_language);
780
5fe41fbf
TT
781 if (py_bp != NULL)
782 has_func = PyObject_HasAttrString (py_bp, stop_func);
7371cf6d 783
7371cf6d
PM
784 do_cleanups (cleanup);
785
786 return has_func;
787}
788
adc36818
PM
789\f
790
791/* Event callback functions. */
792
793/* Callback that is used when a breakpoint is created. This function
794 will create a new Python breakpoint object. */
795static void
8d3788bd 796gdbpy_breakpoint_created (struct breakpoint *bp)
adc36818
PM
797{
798 breakpoint_object *newbp;
adc36818
PM
799 PyGILState_STATE state;
800
f6d90398 801 if (bp->number < 0 && bppy_pending_object == NULL)
84f4c1fe
PM
802 return;
803
adc36818
PM
804 if (bp->type != bp_breakpoint
805 && bp->type != bp_watchpoint
806 && bp->type != bp_hardware_watchpoint
807 && bp->type != bp_read_watchpoint
808 && bp->type != bp_access_watchpoint)
809 return;
810
adc36818
PM
811 state = PyGILState_Ensure ();
812
813 if (bppy_pending_object)
814 {
815 newbp = bppy_pending_object;
816 bppy_pending_object = NULL;
817 }
818 else
819 newbp = PyObject_New (breakpoint_object, &breakpoint_object_type);
820 if (newbp)
821 {
8d3788bd 822 newbp->number = bp->number;
adc36818 823 newbp->bp = bp;
50389644 824 newbp->bp->py_bp_object = newbp;
cc72b2a2 825 newbp->is_finish_bp = 0;
adc36818 826 Py_INCREF (newbp);
84f4c1fe
PM
827 ++bppy_live;
828 }
829 else
830 {
831 PyErr_SetString (PyExc_RuntimeError,
832 _("Error while creating breakpoint from GDB."));
833 gdbpy_print_stack ();
adc36818 834 }
adc36818
PM
835
836 PyGILState_Release (state);
837}
838
839/* Callback that is used when a breakpoint is deleted. This will
840 invalidate the corresponding Python object. */
841static void
8d3788bd 842gdbpy_breakpoint_deleted (struct breakpoint *b)
adc36818 843{
8d3788bd 844 int num = b->number;
adc36818 845 PyGILState_STATE state;
84f4c1fe
PM
846 struct breakpoint *bp = NULL;
847 breakpoint_object *bp_obj;
adc36818
PM
848
849 state = PyGILState_Ensure ();
84f4c1fe 850 bp = get_breakpoint (num);
d930d06e 851 if (bp)
adc36818 852 {
d930d06e
TT
853 bp_obj = bp->py_bp_object;
854 if (bp_obj)
855 {
856 bp_obj->bp = NULL;
857 --bppy_live;
858 Py_DECREF (bp_obj);
859 }
adc36818
PM
860 }
861 PyGILState_Release (state);
862}
863
864\f
865
866/* Initialize the Python breakpoint code. */
999633ed 867int
adc36818
PM
868gdbpy_initialize_breakpoints (void)
869{
870 int i;
871
6a1b1664 872 breakpoint_object_type.tp_new = PyType_GenericNew;
adc36818 873 if (PyType_Ready (&breakpoint_object_type) < 0)
999633ed 874 return -1;
adc36818 875
aa36459a
TT
876 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
877 (PyObject *) &breakpoint_object_type) < 0)
999633ed 878 return -1;
adc36818
PM
879
880 observer_attach_breakpoint_created (gdbpy_breakpoint_created);
881 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
882
883 /* Add breakpoint types constants. */
884 for (i = 0; pybp_codes[i].name; ++i)
885 {
886 if (PyModule_AddIntConstant (gdb_module,
887 /* Cast needed for Python 2.4. */
888 (char *) pybp_codes[i].name,
889 pybp_codes[i].code) < 0)
999633ed 890 return -1;
adc36818
PM
891 }
892
893 /* Add watchpoint types constants. */
894 for (i = 0; pybp_watch_types[i].name; ++i)
895 {
896 if (PyModule_AddIntConstant (gdb_module,
897 /* Cast needed for Python 2.4. */
898 (char *) pybp_watch_types[i].name,
899 pybp_watch_types[i].code) < 0)
999633ed 900 return -1;
adc36818
PM
901 }
902
999633ed 903 return 0;
adc36818
PM
904}
905
906\f
907
7371cf6d
PM
908/* Helper function that overrides this Python object's
909 PyObject_GenericSetAttr to allow extra validation of the attribute
910 being set. */
911
912static int
913local_setattro (PyObject *self, PyObject *name, PyObject *v)
914{
915 breakpoint_object *obj = (breakpoint_object *) self;
916 char *attr = python_string_to_host_string (name);
917
918 if (attr == NULL)
919 return -1;
920
921 /* If the attribute trying to be set is the "stop" method,
922 but we already have a condition set in the CLI, disallow this
923 operation. */
924 if (strcmp (attr, stop_func) == 0 && obj->bp->cond_string)
925 {
926 xfree (attr);
927 PyErr_SetString (PyExc_RuntimeError,
928 _("Cannot set 'stop' method. There is an " \
929 "existing GDB condition attached to the " \
930 "breakpoint."));
931 return -1;
932 }
933
934 xfree (attr);
935
936 return PyObject_GenericSetAttr ((PyObject *)self, name, v);
937}
938
adc36818
PM
939static PyGetSetDef breakpoint_object_getset[] = {
940 { "enabled", bppy_get_enabled, bppy_set_enabled,
941 "Boolean telling whether the breakpoint is enabled.", NULL },
942 { "silent", bppy_get_silent, bppy_set_silent,
943 "Boolean telling whether the breakpoint is silent.", NULL },
944 { "thread", bppy_get_thread, bppy_set_thread,
945 "Thread ID for the breakpoint.\n\
946If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
947If the value is None, then this breakpoint is not thread-specific.\n\
948No other type of value can be used.", NULL },
949 { "task", bppy_get_task, bppy_set_task,
950 "Thread ID for the breakpoint.\n\
951If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
952If the value is None, then this breakpoint is not task-specific.\n\
953No other type of value can be used.", NULL },
954 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
955 "Number of times this breakpoint should be automatically continued.",
956 NULL },
957 { "number", bppy_get_number, NULL,
958 "Breakpoint's number assigned by GDB.", NULL },
959 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
960 "Number of times the breakpoint has been hit.\n\
961Can be set to zero to clear the count. No other value is valid\n\
962when setting this property.", NULL },
963 { "location", bppy_get_location, NULL,
964 "Location of the breakpoint, as specified by the user.", NULL},
965 { "expression", bppy_get_expression, NULL,
966 "Expression of the breakpoint, as specified by the user.", NULL},
967 { "condition", bppy_get_condition, bppy_set_condition,
968 "Condition of the breakpoint, as specified by the user,\
969or None if no condition set."},
970 { "commands", bppy_get_commands, NULL,
971 "Commands of the breakpoint, as specified by the user."},
972 { "type", bppy_get_type, NULL,
973 "Type of breakpoint."},
84f4c1fe
PM
974 { "visible", bppy_get_visibility, NULL,
975 "Whether the breakpoint is visible to the user."},
adc36818
PM
976 { NULL } /* Sentinel. */
977};
978
979static PyMethodDef breakpoint_object_methods[] =
980{
981 { "is_valid", bppy_is_valid, METH_NOARGS,
982 "Return true if this breakpoint is valid, false if not." },
94b6973e
PM
983 { "delete", bppy_delete_breakpoint, METH_NOARGS,
984 "Delete the underlying GDB breakpoint." },
adc36818
PM
985 { NULL } /* Sentinel. */
986};
987
cc72b2a2 988PyTypeObject breakpoint_object_type =
adc36818 989{
9a27f2c6 990 PyVarObject_HEAD_INIT (NULL, 0)
adc36818
PM
991 "gdb.Breakpoint", /*tp_name*/
992 sizeof (breakpoint_object), /*tp_basicsize*/
993 0, /*tp_itemsize*/
994 0, /*tp_dealloc*/
995 0, /*tp_print*/
996 0, /*tp_getattr*/
997 0, /*tp_setattr*/
998 0, /*tp_compare*/
999 0, /*tp_repr*/
1000 0, /*tp_as_number*/
1001 0, /*tp_as_sequence*/
1002 0, /*tp_as_mapping*/
1003 0, /*tp_hash */
1004 0, /*tp_call*/
1005 0, /*tp_str*/
1006 0, /*tp_getattro*/
7371cf6d 1007 (setattrofunc)local_setattro, /*tp_setattro */
adc36818 1008 0, /*tp_as_buffer*/
7371cf6d 1009 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
adc36818
PM
1010 "GDB breakpoint object", /* tp_doc */
1011 0, /* tp_traverse */
1012 0, /* tp_clear */
1013 0, /* tp_richcompare */
1014 0, /* tp_weaklistoffset */
1015 0, /* tp_iter */
1016 0, /* tp_iternext */
1017 breakpoint_object_methods, /* tp_methods */
1018 0, /* tp_members */
7371cf6d
PM
1019 breakpoint_object_getset, /* tp_getset */
1020 0, /* tp_base */
1021 0, /* tp_dict */
1022 0, /* tp_descr_get */
1023 0, /* tp_descr_set */
1024 0, /* tp_dictoffset */
1025 bppy_init, /* tp_init */
1026 0, /* tp_alloc */
adc36818 1027};
This page took 0.377968 seconds and 4 git commands to generate.