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