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