Add Python rbreak command.
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
CommitLineData
adc36818
PM
1/* Python interface to breakpoints
2
61baf725 3 Copyright (C) 2008-2017 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 43/* Function that is called when a Python condition is evaluated. */
a121b7c1 44static const char stop_func[] = "stop";
7371cf6d 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
d28cd78a 393 str = event_location_to_string (obj->bp->location.get ());
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{
a121b7c1 403 const 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{
9b972014
TT
443 gdb::unique_xmalloc_ptr<char> exp_holder;
444 const char *exp = NULL;
4cb0213d 445 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
492d29ea 446 struct gdb_exception except = exception_none;
adc36818
PM
447
448 BPPY_SET_REQUIRE_VALID (self_bp);
449
450 if (newvalue == NULL)
451 {
256458bc 452 PyErr_SetString (PyExc_TypeError,
adc36818
PM
453 _("Cannot delete `condition' attribute."));
454 return -1;
455 }
456 else if (newvalue == Py_None)
457 exp = "";
458 else
459 {
9b972014
TT
460 exp_holder = python_string_to_host_string (newvalue);
461 if (exp_holder == NULL)
adc36818 462 return -1;
9b972014 463 exp = exp_holder.get ();
adc36818
PM
464 }
465
492d29ea 466 TRY
adc36818
PM
467 {
468 set_breakpoint_condition (self_bp->bp, exp, 0);
469 }
492d29ea
PA
470 CATCH (ex, RETURN_MASK_ALL)
471 {
472 except = ex;
473 }
474 END_CATCH
f3be5b64 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 488 PyObject *result;
adc36818
PM
489
490 BPPY_REQUIRE_VALID (self_bp);
491
492 if (! self_bp->bp->commands)
493 Py_RETURN_NONE;
494
d7e74731 495 string_file stb;
adc36818 496
d7e74731 497 current_uiout->redirect (&stb);
492d29ea 498 TRY
adc36818 499 {
79a45e25 500 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
adc36818 501 }
492d29ea 502 CATCH (except, RETURN_MASK_ALL)
b862ce75 503 {
112e8700 504 current_uiout->redirect (NULL);
f3300387
TT
505 gdbpy_convert_exception (except);
506 return NULL;
b862ce75 507 }
492d29ea 508 END_CATCH
adc36818 509
112e8700 510 current_uiout->redirect (NULL);
d7e74731 511 return host_string_to_python_string (stb.c_str ());
adc36818
PM
512}
513
514/* Python function to get the breakpoint type. */
515static PyObject *
516bppy_get_type (PyObject *self, void *closure)
517{
4cb0213d 518 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
519
520 BPPY_REQUIRE_VALID (self_bp);
521
522 return PyInt_FromLong (self_bp->bp->type);
523}
524
84f4c1fe
PM
525/* Python function to get the visibility of the breakpoint. */
526
527static PyObject *
528bppy_get_visibility (PyObject *self, void *closure)
529{
4cb0213d 530 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
84f4c1fe
PM
531
532 BPPY_REQUIRE_VALID (self_bp);
533
43684a7b
TT
534 if (user_breakpoint_p (self_bp->bp))
535 Py_RETURN_TRUE;
84f4c1fe 536
43684a7b 537 Py_RETURN_FALSE;
84f4c1fe
PM
538}
539
f76c27b5
PM
540/* Python function to determine if the breakpoint is a temporary
541 breakpoint. */
542
543static PyObject *
544bppy_get_temporary (PyObject *self, void *closure)
545{
4cb0213d 546 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
f76c27b5
PM
547
548 BPPY_REQUIRE_VALID (self_bp);
549
550 if (self_bp->bp->disposition == disp_del
551 || self_bp->bp->disposition == disp_del_at_next_stop)
552 Py_RETURN_TRUE;
553
554 Py_RETURN_FALSE;
555}
556
93daf339
TT
557/* Python function to determine if the breakpoint is a pending
558 breakpoint. */
559
560static PyObject *
561bppy_get_pending (PyObject *self, void *closure)
562{
563 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
564
565 BPPY_REQUIRE_VALID (self_bp);
566
567 if (is_watchpoint (self_bp->bp))
568 Py_RETURN_FALSE;
569 if (pending_breakpoint_p (self_bp->bp))
570 Py_RETURN_TRUE;
571
572 Py_RETURN_FALSE;
573}
574
adc36818
PM
575/* Python function to get the breakpoint's number. */
576static PyObject *
577bppy_get_number (PyObject *self, void *closure)
578{
4cb0213d 579 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
580
581 BPPY_REQUIRE_VALID (self_bp);
582
583 return PyInt_FromLong (self_bp->number);
584}
585
586/* Python function to get the breakpoint's thread ID. */
587static PyObject *
588bppy_get_thread (PyObject *self, void *closure)
589{
4cb0213d 590 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
591
592 BPPY_REQUIRE_VALID (self_bp);
593
594 if (self_bp->bp->thread == -1)
595 Py_RETURN_NONE;
596
597 return PyInt_FromLong (self_bp->bp->thread);
598}
599
600/* Python function to get the breakpoint's task ID (in Ada). */
601static PyObject *
602bppy_get_task (PyObject *self, void *closure)
603{
4cb0213d 604 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
605
606 BPPY_REQUIRE_VALID (self_bp);
607
608 if (self_bp->bp->task == 0)
609 Py_RETURN_NONE;
610
611 return PyInt_FromLong (self_bp->bp->task);
612}
613
614/* Python function to get the breakpoint's hit count. */
615static PyObject *
616bppy_get_hit_count (PyObject *self, void *closure)
617{
4cb0213d 618 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
619
620 BPPY_REQUIRE_VALID (self_bp);
621
622 return PyInt_FromLong (self_bp->bp->hit_count);
623}
624
625/* Python function to get the breakpoint's ignore count. */
626static PyObject *
627bppy_get_ignore_count (PyObject *self, void *closure)
628{
4cb0213d 629 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
630
631 BPPY_REQUIRE_VALID (self_bp);
632
633 return PyInt_FromLong (self_bp->bp->ignore_count);
634}
635
636/* Python function to create a new breakpoint. */
7371cf6d
PM
637static int
638bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
adc36818 639{
2adadf51
PA
640 static const char *keywords[] = { "spec", "type", "wp_class", "internal",
641 "temporary", NULL };
ddd49eee 642 const char *spec;
adc36818
PM
643 int type = bp_breakpoint;
644 int access_type = hw_write;
84f4c1fe 645 PyObject *internal = NULL;
f76c27b5 646 PyObject *temporary = NULL;
84f4c1fe 647 int internal_bp = 0;
f76c27b5 648 int temporary_bp = 0;
adc36818 649
2adadf51
PA
650 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
651 &spec, &type, &access_type,
652 &internal, &temporary))
7371cf6d 653 return -1;
adc36818 654
84f4c1fe
PM
655 if (internal)
656 {
657 internal_bp = PyObject_IsTrue (internal);
658 if (internal_bp == -1)
7371cf6d 659 return -1;
84f4c1fe
PM
660 }
661
f76c27b5
PM
662 if (temporary != NULL)
663 {
664 temporary_bp = PyObject_IsTrue (temporary);
665 if (temporary_bp == -1)
666 return -1;
667 }
668
4cb0213d 669 bppy_pending_object = (gdbpy_breakpoint_object *) self;
adc36818
PM
670 bppy_pending_object->number = -1;
671 bppy_pending_object->bp = NULL;
256458bc 672
492d29ea 673 TRY
adc36818 674 {
9b972014 675 gdb::unique_xmalloc_ptr<char>
f1735a53 676 copy_holder (xstrdup (skip_spaces (spec)));
f2fc3015 677 const char *copy = copy_holder.get ();
ddd49eee 678
adc36818
PM
679 switch (type)
680 {
681 case bp_breakpoint:
682 {
ffc2605c 683 event_location_up location
9f61929f 684 = string_to_event_location_basic (&copy, current_language);
adc36818 685 create_breakpoint (python_gdbarch,
ffc2605c 686 location.get (), NULL, -1, NULL,
adc36818 687 0,
f76c27b5 688 temporary_bp, bp_breakpoint,
adc36818
PM
689 0,
690 AUTO_BOOLEAN_TRUE,
348d480f 691 &bkpt_breakpoint_ops,
44f238bb 692 0, 1, internal_bp, 0);
adc36818
PM
693 break;
694 }
695 case bp_watchpoint:
696 {
697 if (access_type == hw_write)
ddd49eee 698 watch_command_wrapper (copy, 0, internal_bp);
adc36818 699 else if (access_type == hw_access)
ddd49eee 700 awatch_command_wrapper (copy, 0, internal_bp);
adc36818 701 else if (access_type == hw_read)
ddd49eee 702 rwatch_command_wrapper (copy, 0, internal_bp);
adc36818
PM
703 else
704 error(_("Cannot understand watchpoint access type."));
705 break;
706 }
707 default:
708 error(_("Do not understand breakpoint type to set."));
709 }
710 }
492d29ea 711 CATCH (except, RETURN_MASK_ALL)
adc36818 712 {
f4952523 713 bppy_pending_object = NULL;
7371cf6d
PM
714 PyErr_Format (except.reason == RETURN_QUIT
715 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
716 "%s", except.message);
717 return -1;
adc36818 718 }
492d29ea 719 END_CATCH
adc36818 720
4cb0213d 721 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
7371cf6d 722 return 0;
adc36818
PM
723}
724
725\f
726
84f4c1fe
PM
727static int
728build_bp_list (struct breakpoint *b, void *arg)
729{
19ba03f4 730 PyObject *list = (PyObject *) arg;
50389644 731 PyObject *bp = (PyObject *) b->py_bp_object;
84f4c1fe
PM
732 int iserr = 0;
733
734 /* Not all breakpoints will have a companion Python object.
735 Only breakpoints that were created via bppy_new, or
736 breakpoints that were created externally and are tracked by
737 the Python Scripting API. */
738 if (bp)
739 iserr = PyList_Append (list, bp);
740
741 if (iserr == -1)
742 return 1;
743
744 return 0;
745}
746
adc36818
PM
747/* Static function to return a tuple holding all breakpoints. */
748
749PyObject *
750gdbpy_breakpoints (PyObject *self, PyObject *args)
751{
adc36818 752 if (bppy_live == 0)
1957f6b8 753 return PyTuple_New (0);
adc36818 754
7780f186 755 gdbpy_ref<> list (PyList_New (0));
bf2a52fa 756 if (list == NULL)
84f4c1fe 757 return NULL;
d59b6f6c 758
1957f6b8 759 /* If iterate_over_breakpoints returns non NULL it signals an error
84f4c1fe
PM
760 condition. In that case abandon building the list and return
761 NULL. */
bf2a52fa
TT
762 if (iterate_over_breakpoints (build_bp_list, list.get ()) != NULL)
763 return NULL;
27ca1a5b 764
bf2a52fa 765 return PyList_AsTuple (list.get ());
adc36818
PM
766}
767
7371cf6d
PM
768/* Call the "stop" method (if implemented) in the breakpoint
769 class. If the method returns True, the inferior will be
770 stopped at the breakpoint. Otherwise the inferior will be
771 allowed to continue. */
772
6dddc817
DE
773enum ext_lang_bp_stop
774gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
775 struct breakpoint *b)
7371cf6d 776{
6dddc817
DE
777 int stop;
778 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
7371cf6d 779 PyObject *py_bp = (PyObject *) bp_obj;
6dddc817 780 struct gdbarch *garch;
6dddc817
DE
781
782 if (bp_obj == NULL)
783 return EXT_LANG_BP_STOP_UNSET;
784
785 stop = -1;
786 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
de2dc875
TT
787
788 gdbpy_enter enter_py (garch, current_language);
7371cf6d 789
cc72b2a2
KP
790 if (bp_obj->is_finish_bp)
791 bpfinishpy_pre_stop_hook (bp_obj);
792
7371cf6d
PM
793 if (PyObject_HasAttrString (py_bp, stop_func))
794 {
7780f186 795 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
7371cf6d 796
6dddc817 797 stop = 1;
64081434 798 if (result != NULL)
7371cf6d 799 {
64081434 800 int evaluate = PyObject_IsTrue (result.get ());
7371cf6d
PM
801
802 if (evaluate == -1)
803 gdbpy_print_stack ();
804
805 /* If the "stop" function returns False that means
806 the Python breakpoint wants GDB to continue. */
807 if (! evaluate)
808 stop = 0;
7371cf6d
PM
809 }
810 else
811 gdbpy_print_stack ();
812 }
cc72b2a2
KP
813
814 if (bp_obj->is_finish_bp)
815 bpfinishpy_post_stop_hook (bp_obj);
816
6dddc817
DE
817 if (stop < 0)
818 return EXT_LANG_BP_STOP_UNSET;
819 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
7371cf6d
PM
820}
821
822/* Checks if the "stop" method exists in this breakpoint.
823 Used by condition_command to ensure mutual exclusion of breakpoint
824 conditions. */
825
826int
6dddc817
DE
827gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
828 struct breakpoint *b)
7371cf6d 829{
6dddc817
DE
830 PyObject *py_bp;
831 struct gdbarch *garch;
256458bc 832
6dddc817
DE
833 if (b->py_bp_object == NULL)
834 return 0;
7371cf6d 835
6dddc817
DE
836 py_bp = (PyObject *) b->py_bp_object;
837 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
7371cf6d 838
de2dc875
TT
839 gdbpy_enter enter_py (garch, current_language);
840 return PyObject_HasAttrString (py_bp, stop_func);
7371cf6d
PM
841}
842
adc36818
PM
843\f
844
845/* Event callback functions. */
846
847/* Callback that is used when a breakpoint is created. This function
848 will create a new Python breakpoint object. */
849static void
8d3788bd 850gdbpy_breakpoint_created (struct breakpoint *bp)
adc36818 851{
4cb0213d 852 gdbpy_breakpoint_object *newbp;
adc36818
PM
853 PyGILState_STATE state;
854
43684a7b 855 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
84f4c1fe
PM
856 return;
857
256458bc 858 if (bp->type != bp_breakpoint
adc36818 859 && bp->type != bp_watchpoint
256458bc 860 && bp->type != bp_hardware_watchpoint
adc36818
PM
861 && bp->type != bp_read_watchpoint
862 && bp->type != bp_access_watchpoint)
863 return;
864
adc36818
PM
865 state = PyGILState_Ensure ();
866
867 if (bppy_pending_object)
868 {
869 newbp = bppy_pending_object;
870 bppy_pending_object = NULL;
871 }
872 else
4cb0213d 873 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
adc36818
PM
874 if (newbp)
875 {
8d3788bd 876 newbp->number = bp->number;
adc36818 877 newbp->bp = bp;
50389644 878 newbp->bp->py_bp_object = newbp;
cc72b2a2 879 newbp->is_finish_bp = 0;
adc36818 880 Py_INCREF (newbp);
84f4c1fe
PM
881 ++bppy_live;
882 }
883 else
884 {
885 PyErr_SetString (PyExc_RuntimeError,
886 _("Error while creating breakpoint from GDB."));
887 gdbpy_print_stack ();
adc36818 888 }
adc36818 889
dac790e1
TT
890 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
891 {
dac790e1
TT
892 if (evpy_emit_event ((PyObject *) newbp,
893 gdb_py_events.breakpoint_created) < 0)
894 gdbpy_print_stack ();
895 }
896
adc36818
PM
897 PyGILState_Release (state);
898}
899
900/* Callback that is used when a breakpoint is deleted. This will
901 invalidate the corresponding Python object. */
902static void
8d3788bd 903gdbpy_breakpoint_deleted (struct breakpoint *b)
adc36818 904{
8d3788bd 905 int num = b->number;
adc36818 906 PyGILState_STATE state;
84f4c1fe 907 struct breakpoint *bp = NULL;
adc36818
PM
908
909 state = PyGILState_Ensure ();
84f4c1fe 910 bp = get_breakpoint (num);
d930d06e 911 if (bp)
adc36818 912 {
88b6faea
TT
913 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
914 if (bp_obj != NULL)
d930d06e 915 {
dac790e1
TT
916 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
917 {
88b6faea 918 if (evpy_emit_event ((PyObject *) bp_obj.get (),
dac790e1
TT
919 gdb_py_events.breakpoint_deleted) < 0)
920 gdbpy_print_stack ();
921 }
922
d930d06e
TT
923 bp_obj->bp = NULL;
924 --bppy_live;
d930d06e 925 }
adc36818
PM
926 }
927 PyGILState_Release (state);
928}
929
dac790e1
TT
930/* Callback that is used when a breakpoint is modified. */
931
932static void
933gdbpy_breakpoint_modified (struct breakpoint *b)
934{
935 int num = b->number;
936 PyGILState_STATE state;
937 struct breakpoint *bp = NULL;
938 gdbpy_breakpoint_object *bp_obj;
939
940 state = PyGILState_Ensure ();
941 bp = get_breakpoint (num);
942 if (bp)
943 {
944 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
945 if (bp_obj)
946 {
947 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
948 {
dac790e1
TT
949 if (evpy_emit_event (bp_obj,
950 gdb_py_events.breakpoint_modified) < 0)
951 gdbpy_print_stack ();
952 }
953 }
954 }
955 PyGILState_Release (state);
956}
957
adc36818
PM
958\f
959
960/* Initialize the Python breakpoint code. */
999633ed 961int
adc36818
PM
962gdbpy_initialize_breakpoints (void)
963{
964 int i;
965
6a1b1664 966 breakpoint_object_type.tp_new = PyType_GenericNew;
adc36818 967 if (PyType_Ready (&breakpoint_object_type) < 0)
999633ed 968 return -1;
adc36818 969
aa36459a
TT
970 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
971 (PyObject *) &breakpoint_object_type) < 0)
999633ed 972 return -1;
adc36818
PM
973
974 observer_attach_breakpoint_created (gdbpy_breakpoint_created);
975 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
dac790e1 976 observer_attach_breakpoint_modified (gdbpy_breakpoint_modified);
adc36818
PM
977
978 /* Add breakpoint types constants. */
979 for (i = 0; pybp_codes[i].name; ++i)
980 {
981 if (PyModule_AddIntConstant (gdb_module,
982 /* Cast needed for Python 2.4. */
983 (char *) pybp_codes[i].name,
984 pybp_codes[i].code) < 0)
999633ed 985 return -1;
adc36818
PM
986 }
987
988 /* Add watchpoint types constants. */
989 for (i = 0; pybp_watch_types[i].name; ++i)
990 {
991 if (PyModule_AddIntConstant (gdb_module,
992 /* Cast needed for Python 2.4. */
993 (char *) pybp_watch_types[i].name,
994 pybp_watch_types[i].code) < 0)
999633ed 995 return -1;
adc36818
PM
996 }
997
999633ed 998 return 0;
adc36818
PM
999}
1000
1001\f
1002
7371cf6d
PM
1003/* Helper function that overrides this Python object's
1004 PyObject_GenericSetAttr to allow extra validation of the attribute
1005 being set. */
1006
256458bc 1007static int
7371cf6d
PM
1008local_setattro (PyObject *self, PyObject *name, PyObject *v)
1009{
256458bc 1010 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
9b972014 1011 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
256458bc 1012
7371cf6d
PM
1013 if (attr == NULL)
1014 return -1;
256458bc 1015
7371cf6d 1016 /* If the attribute trying to be set is the "stop" method,
6dddc817
DE
1017 but we already have a condition set in the CLI or other extension
1018 language, disallow this operation. */
9b972014 1019 if (strcmp (attr.get (), stop_func) == 0)
7371cf6d 1020 {
6dddc817
DE
1021 const struct extension_language_defn *extlang = NULL;
1022
1023 if (obj->bp->cond_string != NULL)
1024 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1025 if (extlang == NULL)
1026 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1027 if (extlang != NULL)
1028 {
7f968c89
TT
1029 std::string error_text
1030 = string_printf (_("Only one stop condition allowed. There is"
1031 " currently a %s stop condition defined for"
1032 " this breakpoint."),
1033 ext_lang_capitalized_name (extlang));
1034 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
6dddc817
DE
1035 return -1;
1036 }
7371cf6d 1037 }
256458bc 1038
256458bc 1039 return PyObject_GenericSetAttr ((PyObject *)self, name, v);
7371cf6d
PM
1040}
1041
0d1f4ceb 1042static gdb_PyGetSetDef breakpoint_object_getset[] = {
adc36818
PM
1043 { "enabled", bppy_get_enabled, bppy_set_enabled,
1044 "Boolean telling whether the breakpoint is enabled.", NULL },
1045 { "silent", bppy_get_silent, bppy_set_silent,
1046 "Boolean telling whether the breakpoint is silent.", NULL },
1047 { "thread", bppy_get_thread, bppy_set_thread,
1048 "Thread ID for the breakpoint.\n\
1049If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1050If the value is None, then this breakpoint is not thread-specific.\n\
1051No other type of value can be used.", NULL },
1052 { "task", bppy_get_task, bppy_set_task,
1053 "Thread ID for the breakpoint.\n\
1054If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1055If the value is None, then this breakpoint is not task-specific.\n\
1056No other type of value can be used.", NULL },
1057 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1058 "Number of times this breakpoint should be automatically continued.",
1059 NULL },
1060 { "number", bppy_get_number, NULL,
1061 "Breakpoint's number assigned by GDB.", NULL },
1062 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1063 "Number of times the breakpoint has been hit.\n\
1064Can be set to zero to clear the count. No other value is valid\n\
1065when setting this property.", NULL },
1066 { "location", bppy_get_location, NULL,
1067 "Location of the breakpoint, as specified by the user.", NULL},
1068 { "expression", bppy_get_expression, NULL,
1069 "Expression of the breakpoint, as specified by the user.", NULL},
1070 { "condition", bppy_get_condition, bppy_set_condition,
1071 "Condition of the breakpoint, as specified by the user,\
1072or None if no condition set."},
1073 { "commands", bppy_get_commands, NULL,
1074 "Commands of the breakpoint, as specified by the user."},
1075 { "type", bppy_get_type, NULL,
1076 "Type of breakpoint."},
84f4c1fe
PM
1077 { "visible", bppy_get_visibility, NULL,
1078 "Whether the breakpoint is visible to the user."},
f76c27b5
PM
1079 { "temporary", bppy_get_temporary, NULL,
1080 "Whether this breakpoint is a temporary breakpoint."},
93daf339
TT
1081 { "pending", bppy_get_pending, NULL,
1082 "Whether this breakpoint is a pending breakpoint."},
adc36818
PM
1083 { NULL } /* Sentinel. */
1084};
1085
1086static PyMethodDef breakpoint_object_methods[] =
1087{
1088 { "is_valid", bppy_is_valid, METH_NOARGS,
1089 "Return true if this breakpoint is valid, false if not." },
94b6973e
PM
1090 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1091 "Delete the underlying GDB breakpoint." },
adc36818
PM
1092 { NULL } /* Sentinel. */
1093};
1094
cc72b2a2 1095PyTypeObject breakpoint_object_type =
adc36818 1096{
9a27f2c6 1097 PyVarObject_HEAD_INIT (NULL, 0)
adc36818 1098 "gdb.Breakpoint", /*tp_name*/
4cb0213d 1099 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
adc36818
PM
1100 0, /*tp_itemsize*/
1101 0, /*tp_dealloc*/
1102 0, /*tp_print*/
1103 0, /*tp_getattr*/
1104 0, /*tp_setattr*/
1105 0, /*tp_compare*/
1106 0, /*tp_repr*/
1107 0, /*tp_as_number*/
1108 0, /*tp_as_sequence*/
1109 0, /*tp_as_mapping*/
1110 0, /*tp_hash */
1111 0, /*tp_call*/
1112 0, /*tp_str*/
1113 0, /*tp_getattro*/
7371cf6d 1114 (setattrofunc)local_setattro, /*tp_setattro */
adc36818 1115 0, /*tp_as_buffer*/
7371cf6d 1116 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
adc36818
PM
1117 "GDB breakpoint object", /* tp_doc */
1118 0, /* tp_traverse */
1119 0, /* tp_clear */
1120 0, /* tp_richcompare */
1121 0, /* tp_weaklistoffset */
1122 0, /* tp_iter */
1123 0, /* tp_iternext */
1124 breakpoint_object_methods, /* tp_methods */
1125 0, /* tp_members */
7371cf6d
PM
1126 breakpoint_object_getset, /* tp_getset */
1127 0, /* tp_base */
1128 0, /* tp_dict */
1129 0, /* tp_descr_get */
1130 0, /* tp_descr_set */
1131 0, /* tp_dictoffset */
1132 bppy_init, /* tp_init */
1133 0, /* tp_alloc */
adc36818 1134};
This page took 0.807303 seconds and 4 git commands to generate.