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