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