Fix comment about the signature of add_separate_debug_file
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
CommitLineData
adc36818
PM
1/* Python interface to breakpoints
2
42a4f53d 3 Copyright (C) 2008-2019 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"
76727919 28#include "observable.h"
adc36818
PM
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"
824cc835 35#include "linespec.h"
adc36818 36
adc36818
PM
37/* Number of live breakpoints. */
38static int bppy_live;
39
40/* Variables used to pass information between the Breakpoint
41 constructor and the breakpoint-created hook function. */
4cb0213d 42gdbpy_breakpoint_object *bppy_pending_object;
adc36818 43
7371cf6d 44/* Function that is called when a Python condition is evaluated. */
a121b7c1 45static const char stop_func[] = "stop";
7371cf6d 46
adc36818
PM
47/* This is used to initialize various gdb.bp_* constants. */
48struct pybp_code
49{
50 /* The name. */
51 const char *name;
52 /* The code. */
e81b7af8 53 int code;
adc36818
PM
54};
55
56/* Entries related to the type of user set breakpoints. */
57static struct pybp_code pybp_codes[] =
58{
59 { "BP_NONE", bp_none},
60 { "BP_BREAKPOINT", bp_breakpoint},
61 { "BP_WATCHPOINT", bp_watchpoint},
62 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
63 { "BP_READ_WATCHPOINT", bp_read_watchpoint},
64 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
65 {NULL} /* Sentinel. */
66};
67
68/* Entries related to the type of watchpoint. */
69static struct pybp_code pybp_watch_types[] =
70{
71 { "WP_READ", hw_read},
72 { "WP_WRITE", hw_write},
73 { "WP_ACCESS", hw_access},
74 {NULL} /* Sentinel. */
75};
76
adc36818
PM
77/* Python function which checks the validity of a breakpoint object. */
78static PyObject *
79bppy_is_valid (PyObject *self, PyObject *args)
80{
4cb0213d 81 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
82
83 if (self_bp->bp)
84 Py_RETURN_TRUE;
85 Py_RETURN_FALSE;
86}
87
88/* Python function to test whether or not the breakpoint is enabled. */
89static PyObject *
90bppy_get_enabled (PyObject *self, void *closure)
91{
4cb0213d 92 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
93
94 BPPY_REQUIRE_VALID (self_bp);
95 if (! self_bp->bp)
96 Py_RETURN_FALSE;
97 if (self_bp->bp->enable_state == bp_enabled)
98 Py_RETURN_TRUE;
99 Py_RETURN_FALSE;
100}
101
102/* Python function to test whether or not the breakpoint is silent. */
103static PyObject *
104bppy_get_silent (PyObject *self, void *closure)
105{
4cb0213d 106 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
107
108 BPPY_REQUIRE_VALID (self_bp);
109 if (self_bp->bp->silent)
110 Py_RETURN_TRUE;
111 Py_RETURN_FALSE;
112}
113
114/* Python function to set the enabled state of a breakpoint. */
115static int
116bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
117{
4cb0213d 118 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
119 int cmp;
120
121 BPPY_SET_REQUIRE_VALID (self_bp);
122
123 if (newvalue == NULL)
124 {
256458bc 125 PyErr_SetString (PyExc_TypeError,
adc36818
PM
126 _("Cannot delete `enabled' attribute."));
127
128 return -1;
129 }
130 else if (! PyBool_Check (newvalue))
131 {
132 PyErr_SetString (PyExc_TypeError,
133 _("The value of `enabled' must be a boolean."));
134 return -1;
135 }
136
137 cmp = PyObject_IsTrue (newvalue);
138 if (cmp < 0)
139 return -1;
76dce0be 140
a70b8144 141 try
76dce0be
PM
142 {
143 if (cmp == 1)
144 enable_breakpoint (self_bp->bp);
145 else
146 disable_breakpoint (self_bp->bp);
147 }
230d2906 148 catch (const gdb_exception &except)
492d29ea
PA
149 {
150 GDB_PY_SET_HANDLE_EXCEPTION (except);
151 }
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
a70b8144 249 try
76dce0be
PM
250 {
251 valid_id = valid_task_id (id);
252 }
230d2906 253 catch (const gdb_exception &except)
492d29ea
PA
254 {
255 GDB_PY_SET_HANDLE_EXCEPTION (except);
256 }
76dce0be
PM
257
258 if (! valid_id)
adc36818 259 {
256458bc 260 PyErr_SetString (PyExc_RuntimeError,
adc36818
PM
261 _("Invalid task ID."));
262 return -1;
263 }
264 }
265 else if (newvalue == Py_None)
266 id = 0;
267 else
268 {
269 PyErr_SetString (PyExc_TypeError,
270 _("The value of `task' must be an integer or None."));
271 return -1;
272 }
273
45a43567 274 breakpoint_set_task (self_bp->bp, id);
adc36818
PM
275
276 return 0;
277}
278
94b6973e
PM
279/* Python function which deletes the underlying GDB breakpoint. This
280 triggers the breakpoint_deleted observer which will call
281 gdbpy_breakpoint_deleted; that function cleans up the Python
282 sections. */
283
284static PyObject *
285bppy_delete_breakpoint (PyObject *self, PyObject *args)
286{
4cb0213d 287 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
94b6973e
PM
288
289 BPPY_REQUIRE_VALID (self_bp);
290
a70b8144 291 try
76dce0be
PM
292 {
293 delete_breakpoint (self_bp->bp);
294 }
230d2906 295 catch (const gdb_exception &except)
492d29ea
PA
296 {
297 GDB_PY_HANDLE_EXCEPTION (except);
298 }
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
a70b8144 332 try
5d9c5995
PM
333 {
334 set_ignore_count (self_bp->number, (int) value, 0);
335 }
230d2906 336 catch (const gdb_exception &except)
492d29ea
PA
337 {
338 GDB_PY_SET_HANDLE_EXCEPTION (except);
339 }
adc36818
PM
340
341 return 0;
342}
343
344/* Python function to set the hit count of a breakpoint. */
345static int
346bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
347{
4cb0213d 348 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
349
350 BPPY_SET_REQUIRE_VALID (self_bp);
351
352 if (newvalue == NULL)
353 {
256458bc 354 PyErr_SetString (PyExc_TypeError,
adc36818
PM
355 _("Cannot delete `hit_count' attribute."));
356 return -1;
357 }
74aedc46 358 else
adc36818 359 {
74aedc46
TT
360 long value;
361
362 if (! gdb_py_int_as_long (newvalue, &value))
363 return -1;
364
365 if (value != 0)
366 {
367 PyErr_SetString (PyExc_AttributeError,
368 _("The value of `hit_count' must be zero."));
369 return -1;
370 }
adc36818
PM
371 }
372
373 self_bp->bp->hit_count = 0;
374
375 return 0;
376}
377
378/* Python function to get the location of a breakpoint. */
379static PyObject *
380bppy_get_location (PyObject *self, void *closure)
381{
4cb0213d 382 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
adc36818
PM
383
384 BPPY_REQUIRE_VALID (obj);
385
386 if (obj->bp->type != bp_breakpoint)
387 Py_RETURN_NONE;
388
cb1e4e32 389 const char *str = event_location_to_string (obj->bp->location.get ());
adc36818
PM
390 if (! str)
391 str = "";
833d985d 392 return host_string_to_python_string (str).release ();
adc36818
PM
393}
394
395/* Python function to get the breakpoint expression. */
396static PyObject *
397bppy_get_expression (PyObject *self, void *closure)
398{
a121b7c1 399 const char *str;
4cb0213d 400 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
3a5c3e22 401 struct watchpoint *wp;
adc36818
PM
402
403 BPPY_REQUIRE_VALID (obj);
404
3a5c3e22 405 if (!is_watchpoint (obj->bp))
adc36818
PM
406 Py_RETURN_NONE;
407
3a5c3e22
PA
408 wp = (struct watchpoint *) obj->bp;
409
410 str = wp->exp_string;
adc36818
PM
411 if (! str)
412 str = "";
413
833d985d 414 return host_string_to_python_string (str).release ();
adc36818
PM
415}
416
417/* Python function to get the condition expression of a breakpoint. */
418static PyObject *
419bppy_get_condition (PyObject *self, void *closure)
420{
421 char *str;
4cb0213d 422 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
adc36818
PM
423
424 BPPY_REQUIRE_VALID (obj);
425
426 str = obj->bp->cond_string;
427 if (! str)
428 Py_RETURN_NONE;
429
833d985d 430 return host_string_to_python_string (str).release ();
adc36818
PM
431}
432
8dc78533
JK
433/* Returns 0 on success. Returns -1 on error, with a python exception set.
434 */
435
adc36818
PM
436static int
437bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
438{
9b972014
TT
439 gdb::unique_xmalloc_ptr<char> exp_holder;
440 const char *exp = NULL;
4cb0213d 441 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
cc06b668 442 struct gdb_exception except;
adc36818
PM
443
444 BPPY_SET_REQUIRE_VALID (self_bp);
445
446 if (newvalue == NULL)
447 {
256458bc 448 PyErr_SetString (PyExc_TypeError,
adc36818
PM
449 _("Cannot delete `condition' attribute."));
450 return -1;
451 }
452 else if (newvalue == Py_None)
453 exp = "";
454 else
455 {
9b972014
TT
456 exp_holder = python_string_to_host_string (newvalue);
457 if (exp_holder == NULL)
adc36818 458 return -1;
9b972014 459 exp = exp_holder.get ();
adc36818
PM
460 }
461
a70b8144 462 try
adc36818
PM
463 {
464 set_breakpoint_condition (self_bp->bp, exp, 0);
465 }
94aeb44b 466 catch (gdb_exception &ex)
492d29ea 467 {
94aeb44b 468 except = std::move (ex);
492d29ea 469 }
f3be5b64 470
adc36818
PM
471 GDB_PY_SET_HANDLE_EXCEPTION (except);
472
473 return 0;
474}
475
476/* Python function to get the commands attached to a breakpoint. */
477static PyObject *
478bppy_get_commands (PyObject *self, void *closure)
479{
4cb0213d 480 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818 481 struct breakpoint *bp = self_bp->bp;
adc36818
PM
482
483 BPPY_REQUIRE_VALID (self_bp);
484
485 if (! self_bp->bp->commands)
486 Py_RETURN_NONE;
487
d7e74731 488 string_file stb;
adc36818 489
d7e74731 490 current_uiout->redirect (&stb);
a70b8144 491 try
adc36818 492 {
79a45e25 493 print_command_lines (current_uiout, breakpoint_commands (bp), 0);
adc36818 494 }
230d2906 495 catch (const gdb_exception &except)
b862ce75 496 {
112e8700 497 current_uiout->redirect (NULL);
f3300387
TT
498 gdbpy_convert_exception (except);
499 return NULL;
b862ce75 500 }
adc36818 501
112e8700 502 current_uiout->redirect (NULL);
833d985d 503 return host_string_to_python_string (stb.c_str ()).release ();
adc36818
PM
504}
505
a913fffb
TT
506/* Set the commands attached to a breakpoint. Returns 0 on success.
507 Returns -1 on error, with a python exception set. */
508static int
509bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
510{
511 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
cc06b668 512 struct gdb_exception except;
a913fffb
TT
513
514 BPPY_SET_REQUIRE_VALID (self_bp);
515
516 gdb::unique_xmalloc_ptr<char> commands
517 (python_string_to_host_string (newvalue));
518 if (commands == nullptr)
519 return -1;
520
a70b8144 521 try
a913fffb
TT
522 {
523 bool first = true;
524 char *save_ptr = nullptr;
525 auto reader
526 = [&] ()
527 {
528 const char *result = strtok_r (first ? commands.get () : nullptr,
529 "\n", &save_ptr);
530 first = false;
531 return result;
532 };
533
534 counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
535 breakpoint_set_commands (self_bp->bp, std::move (lines));
536 }
94aeb44b 537 catch (gdb_exception &ex)
a913fffb 538 {
94aeb44b 539 except = std::move (ex);
a913fffb 540 }
a913fffb
TT
541
542 GDB_PY_SET_HANDLE_EXCEPTION (except);
543
544 return 0;
545}
546
adc36818
PM
547/* Python function to get the breakpoint type. */
548static PyObject *
549bppy_get_type (PyObject *self, void *closure)
550{
4cb0213d 551 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
552
553 BPPY_REQUIRE_VALID (self_bp);
554
555 return PyInt_FromLong (self_bp->bp->type);
556}
557
84f4c1fe
PM
558/* Python function to get the visibility of the breakpoint. */
559
560static PyObject *
561bppy_get_visibility (PyObject *self, void *closure)
562{
4cb0213d 563 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
84f4c1fe
PM
564
565 BPPY_REQUIRE_VALID (self_bp);
566
43684a7b
TT
567 if (user_breakpoint_p (self_bp->bp))
568 Py_RETURN_TRUE;
84f4c1fe 569
43684a7b 570 Py_RETURN_FALSE;
84f4c1fe
PM
571}
572
f76c27b5
PM
573/* Python function to determine if the breakpoint is a temporary
574 breakpoint. */
575
576static PyObject *
577bppy_get_temporary (PyObject *self, void *closure)
578{
4cb0213d 579 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
f76c27b5
PM
580
581 BPPY_REQUIRE_VALID (self_bp);
582
583 if (self_bp->bp->disposition == disp_del
584 || self_bp->bp->disposition == disp_del_at_next_stop)
585 Py_RETURN_TRUE;
586
587 Py_RETURN_FALSE;
588}
589
93daf339
TT
590/* Python function to determine if the breakpoint is a pending
591 breakpoint. */
592
593static PyObject *
594bppy_get_pending (PyObject *self, void *closure)
595{
596 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
597
598 BPPY_REQUIRE_VALID (self_bp);
599
600 if (is_watchpoint (self_bp->bp))
601 Py_RETURN_FALSE;
602 if (pending_breakpoint_p (self_bp->bp))
603 Py_RETURN_TRUE;
604
605 Py_RETURN_FALSE;
606}
607
adc36818
PM
608/* Python function to get the breakpoint's number. */
609static PyObject *
610bppy_get_number (PyObject *self, void *closure)
611{
4cb0213d 612 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
613
614 BPPY_REQUIRE_VALID (self_bp);
615
616 return PyInt_FromLong (self_bp->number);
617}
618
619/* Python function to get the breakpoint's thread ID. */
620static PyObject *
621bppy_get_thread (PyObject *self, void *closure)
622{
4cb0213d 623 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
624
625 BPPY_REQUIRE_VALID (self_bp);
626
627 if (self_bp->bp->thread == -1)
628 Py_RETURN_NONE;
629
630 return PyInt_FromLong (self_bp->bp->thread);
631}
632
633/* Python function to get the breakpoint's task ID (in Ada). */
634static PyObject *
635bppy_get_task (PyObject *self, void *closure)
636{
4cb0213d 637 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
638
639 BPPY_REQUIRE_VALID (self_bp);
640
641 if (self_bp->bp->task == 0)
642 Py_RETURN_NONE;
643
644 return PyInt_FromLong (self_bp->bp->task);
645}
646
647/* Python function to get the breakpoint's hit count. */
648static PyObject *
649bppy_get_hit_count (PyObject *self, void *closure)
650{
4cb0213d 651 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
652
653 BPPY_REQUIRE_VALID (self_bp);
654
655 return PyInt_FromLong (self_bp->bp->hit_count);
656}
657
658/* Python function to get the breakpoint's ignore count. */
659static PyObject *
660bppy_get_ignore_count (PyObject *self, void *closure)
661{
4cb0213d 662 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
adc36818
PM
663
664 BPPY_REQUIRE_VALID (self_bp);
665
666 return PyInt_FromLong (self_bp->bp->ignore_count);
667}
668
824cc835
PM
669/* Internal function to validate the Python parameters/keywords
670 provided to bppy_init. */
671
672static int
673bppy_init_validate_args (const char *spec, char *source,
674 char *function, char *label,
675 char *line, enum bptype type)
676{
677 /* If spec is defined, ensure that none of the explicit location
678 keywords are also defined. */
679 if (spec != NULL)
680 {
681 if (source != NULL || function != NULL || label != NULL || line != NULL)
682 {
683 PyErr_SetString (PyExc_RuntimeError,
684 _("Breakpoints specified with spec cannot "
685 "have source, function, label or line defined."));
686 return -1;
687 }
688 }
689 else
690 {
691 /* If spec isn't defined, ensure that the user is not trying to
692 define a watchpoint with an explicit location. */
693 if (type == bp_watchpoint)
694 {
695 PyErr_SetString (PyExc_RuntimeError,
696 _("Watchpoints cannot be set by explicit "
697 "location parameters."));
698 return -1;
699 }
700 else
701 {
702 /* Otherwise, ensure some explicit locations are defined. */
703 if (source == NULL && function == NULL && label == NULL
704 && line == NULL)
705 {
706 PyErr_SetString (PyExc_RuntimeError,
707 _("Neither spec nor explicit location set."));
708 return -1;
709 }
710 /* Finally, if source is specified, ensure that line, label
711 or function are specified too. */
712 if (source != NULL && function == NULL && label == NULL
713 && line == NULL)
714 {
715 PyErr_SetString (PyExc_RuntimeError,
716 _("Specifying a source must also include a "
717 "line, label or function."));
718 return -1;
719 }
720 }
721 }
722 return 1;
723}
724
adc36818 725/* Python function to create a new breakpoint. */
7371cf6d
PM
726static int
727bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
adc36818 728{
2adadf51 729 static const char *keywords[] = { "spec", "type", "wp_class", "internal",
824cc835 730 "temporary","source", "function",
b89641ba 731 "label", "line", "qualified", NULL };
824cc835
PM
732 const char *spec = NULL;
733 enum bptype type = bp_breakpoint;
adc36818 734 int access_type = hw_write;
84f4c1fe 735 PyObject *internal = NULL;
f76c27b5 736 PyObject *temporary = NULL;
824cc835 737 PyObject *lineobj = NULL;;
84f4c1fe 738 int internal_bp = 0;
f76c27b5 739 int temporary_bp = 0;
824cc835
PM
740 gdb::unique_xmalloc_ptr<char> line;
741 char *label = NULL;
742 char *source = NULL;
743 char *function = NULL;
8e557e52 744 PyObject * qualified = NULL;
adc36818 745
8e557e52 746 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
2adadf51 747 &spec, &type, &access_type,
824cc835
PM
748 &internal,
749 &temporary, &source,
b89641ba
SM
750 &function, &label, &lineobj,
751 &qualified))
7371cf6d 752 return -1;
adc36818 753
824cc835
PM
754
755 if (lineobj != NULL)
756 {
757 if (PyInt_Check (lineobj))
758 line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
759 else if (PyString_Check (lineobj))
760 line = python_string_to_host_string (lineobj);
761 else
762 {
763 PyErr_SetString (PyExc_RuntimeError,
764 _("Line keyword should be an integer or a string. "));
765 return -1;
766 }
767 }
768
84f4c1fe
PM
769 if (internal)
770 {
771 internal_bp = PyObject_IsTrue (internal);
772 if (internal_bp == -1)
7371cf6d 773 return -1;
84f4c1fe
PM
774 }
775
f76c27b5
PM
776 if (temporary != NULL)
777 {
778 temporary_bp = PyObject_IsTrue (temporary);
779 if (temporary_bp == -1)
780 return -1;
781 }
782
824cc835
PM
783 if (bppy_init_validate_args (spec, source, function, label, line.get (),
784 type) == -1)
785 return -1;
786
4cb0213d 787 bppy_pending_object = (gdbpy_breakpoint_object *) self;
adc36818
PM
788 bppy_pending_object->number = -1;
789 bppy_pending_object->bp = NULL;
256458bc 790
a70b8144 791 try
adc36818
PM
792 {
793 switch (type)
794 {
795 case bp_breakpoint:
796 {
824cc835 797 event_location_up location;
b89641ba 798 symbol_name_match_type func_name_match_type
8e557e52 799 = (qualified != NULL && PyObject_IsTrue (qualified)
b89641ba
SM
800 ? symbol_name_match_type::FULL
801 : symbol_name_match_type::WILD);
824cc835
PM
802
803 if (spec != NULL)
804 {
805 gdb::unique_xmalloc_ptr<char>
806 copy_holder (xstrdup (skip_spaces (spec)));
807 const char *copy = copy_holder.get ();
808
809 location = string_to_event_location (&copy,
b89641ba
SM
810 current_language,
811 func_name_match_type);
824cc835
PM
812 }
813 else
814 {
815 struct explicit_location explicit_loc;
816
817 initialize_explicit_location (&explicit_loc);
818 explicit_loc.source_filename = source;
819 explicit_loc.function_name = function;
820 explicit_loc.label_name = label;
821
822 if (line != NULL)
823 explicit_loc.line_offset =
824 linespec_parse_line_offset (line.get ());
825
b89641ba
SM
826 explicit_loc.func_name_match_type = func_name_match_type;
827
824cc835
PM
828 location = new_explicit_location (&explicit_loc);
829 }
830
adc36818 831 create_breakpoint (python_gdbarch,
ffc2605c 832 location.get (), NULL, -1, NULL,
adc36818 833 0,
f76c27b5 834 temporary_bp, bp_breakpoint,
adc36818
PM
835 0,
836 AUTO_BOOLEAN_TRUE,
348d480f 837 &bkpt_breakpoint_ops,
44f238bb 838 0, 1, internal_bp, 0);
adc36818
PM
839 break;
840 }
824cc835 841 case bp_watchpoint:
adc36818 842 {
824cc835
PM
843 gdb::unique_xmalloc_ptr<char>
844 copy_holder (xstrdup (skip_spaces (spec)));
845 char *copy = copy_holder.get ();
846
adc36818 847 if (access_type == hw_write)
ddd49eee 848 watch_command_wrapper (copy, 0, internal_bp);
adc36818 849 else if (access_type == hw_access)
ddd49eee 850 awatch_command_wrapper (copy, 0, internal_bp);
adc36818 851 else if (access_type == hw_read)
ddd49eee 852 rwatch_command_wrapper (copy, 0, internal_bp);
adc36818
PM
853 else
854 error(_("Cannot understand watchpoint access type."));
855 break;
856 }
857 default:
858 error(_("Do not understand breakpoint type to set."));
859 }
860 }
230d2906 861 catch (const gdb_exception &except)
adc36818 862 {
f4952523 863 bppy_pending_object = NULL;
ec9c2750 864 gdbpy_convert_exception (except);
7371cf6d 865 return -1;
adc36818
PM
866 }
867
4cb0213d 868 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
7371cf6d 869 return 0;
adc36818
PM
870}
871
872\f
873
84f4c1fe
PM
874static int
875build_bp_list (struct breakpoint *b, void *arg)
876{
19ba03f4 877 PyObject *list = (PyObject *) arg;
50389644 878 PyObject *bp = (PyObject *) b->py_bp_object;
84f4c1fe
PM
879 int iserr = 0;
880
881 /* Not all breakpoints will have a companion Python object.
882 Only breakpoints that were created via bppy_new, or
883 breakpoints that were created externally and are tracked by
884 the Python Scripting API. */
885 if (bp)
886 iserr = PyList_Append (list, bp);
887
888 if (iserr == -1)
889 return 1;
890
891 return 0;
892}
893
adc36818
PM
894/* Static function to return a tuple holding all breakpoints. */
895
896PyObject *
897gdbpy_breakpoints (PyObject *self, PyObject *args)
898{
adc36818 899 if (bppy_live == 0)
1957f6b8 900 return PyTuple_New (0);
adc36818 901
7780f186 902 gdbpy_ref<> list (PyList_New (0));
bf2a52fa 903 if (list == NULL)
84f4c1fe 904 return NULL;
d59b6f6c 905
1957f6b8 906 /* If iterate_over_breakpoints returns non NULL it signals an error
84f4c1fe
PM
907 condition. In that case abandon building the list and return
908 NULL. */
bf2a52fa
TT
909 if (iterate_over_breakpoints (build_bp_list, list.get ()) != NULL)
910 return NULL;
27ca1a5b 911
bf2a52fa 912 return PyList_AsTuple (list.get ());
adc36818
PM
913}
914
7371cf6d
PM
915/* Call the "stop" method (if implemented) in the breakpoint
916 class. If the method returns True, the inferior will be
917 stopped at the breakpoint. Otherwise the inferior will be
918 allowed to continue. */
919
6dddc817
DE
920enum ext_lang_bp_stop
921gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
922 struct breakpoint *b)
7371cf6d 923{
6dddc817
DE
924 int stop;
925 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
7371cf6d 926 PyObject *py_bp = (PyObject *) bp_obj;
6dddc817 927 struct gdbarch *garch;
6dddc817
DE
928
929 if (bp_obj == NULL)
930 return EXT_LANG_BP_STOP_UNSET;
931
932 stop = -1;
933 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
de2dc875
TT
934
935 gdbpy_enter enter_py (garch, current_language);
7371cf6d 936
cc72b2a2
KP
937 if (bp_obj->is_finish_bp)
938 bpfinishpy_pre_stop_hook (bp_obj);
939
7371cf6d
PM
940 if (PyObject_HasAttrString (py_bp, stop_func))
941 {
7780f186 942 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
7371cf6d 943
6dddc817 944 stop = 1;
64081434 945 if (result != NULL)
7371cf6d 946 {
64081434 947 int evaluate = PyObject_IsTrue (result.get ());
7371cf6d
PM
948
949 if (evaluate == -1)
950 gdbpy_print_stack ();
951
952 /* If the "stop" function returns False that means
953 the Python breakpoint wants GDB to continue. */
954 if (! evaluate)
955 stop = 0;
7371cf6d
PM
956 }
957 else
958 gdbpy_print_stack ();
959 }
cc72b2a2
KP
960
961 if (bp_obj->is_finish_bp)
962 bpfinishpy_post_stop_hook (bp_obj);
963
6dddc817
DE
964 if (stop < 0)
965 return EXT_LANG_BP_STOP_UNSET;
966 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
7371cf6d
PM
967}
968
969/* Checks if the "stop" method exists in this breakpoint.
970 Used by condition_command to ensure mutual exclusion of breakpoint
971 conditions. */
972
973int
6dddc817
DE
974gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
975 struct breakpoint *b)
7371cf6d 976{
6dddc817
DE
977 PyObject *py_bp;
978 struct gdbarch *garch;
256458bc 979
6dddc817
DE
980 if (b->py_bp_object == NULL)
981 return 0;
7371cf6d 982
6dddc817
DE
983 py_bp = (PyObject *) b->py_bp_object;
984 garch = b->gdbarch ? b->gdbarch : get_current_arch ();
7371cf6d 985
de2dc875
TT
986 gdbpy_enter enter_py (garch, current_language);
987 return PyObject_HasAttrString (py_bp, stop_func);
7371cf6d
PM
988}
989
adc36818
PM
990\f
991
992/* Event callback functions. */
993
994/* Callback that is used when a breakpoint is created. This function
995 will create a new Python breakpoint object. */
996static void
8d3788bd 997gdbpy_breakpoint_created (struct breakpoint *bp)
adc36818 998{
4cb0213d 999 gdbpy_breakpoint_object *newbp;
adc36818 1000
43684a7b 1001 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
84f4c1fe
PM
1002 return;
1003
256458bc 1004 if (bp->type != bp_breakpoint
adc36818 1005 && bp->type != bp_watchpoint
256458bc 1006 && bp->type != bp_hardware_watchpoint
adc36818
PM
1007 && bp->type != bp_read_watchpoint
1008 && bp->type != bp_access_watchpoint)
1009 return;
1010
25ce02ee
TT
1011 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1012 gdbpy_enter enter_py (garch, current_language);
adc36818
PM
1013
1014 if (bppy_pending_object)
1015 {
1016 newbp = bppy_pending_object;
1017 bppy_pending_object = NULL;
1018 }
1019 else
4cb0213d 1020 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
adc36818
PM
1021 if (newbp)
1022 {
8d3788bd 1023 newbp->number = bp->number;
adc36818 1024 newbp->bp = bp;
50389644 1025 newbp->bp->py_bp_object = newbp;
cc72b2a2 1026 newbp->is_finish_bp = 0;
adc36818 1027 Py_INCREF (newbp);
84f4c1fe
PM
1028 ++bppy_live;
1029 }
1030 else
1031 {
1032 PyErr_SetString (PyExc_RuntimeError,
1033 _("Error while creating breakpoint from GDB."));
1034 gdbpy_print_stack ();
adc36818 1035 }
adc36818 1036
dac790e1
TT
1037 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
1038 {
dac790e1
TT
1039 if (evpy_emit_event ((PyObject *) newbp,
1040 gdb_py_events.breakpoint_created) < 0)
1041 gdbpy_print_stack ();
1042 }
adc36818
PM
1043}
1044
1045/* Callback that is used when a breakpoint is deleted. This will
1046 invalidate the corresponding Python object. */
1047static void
8d3788bd 1048gdbpy_breakpoint_deleted (struct breakpoint *b)
adc36818 1049{
8d3788bd 1050 int num = b->number;
84f4c1fe 1051 struct breakpoint *bp = NULL;
adc36818 1052
84f4c1fe 1053 bp = get_breakpoint (num);
d930d06e 1054 if (bp)
adc36818 1055 {
25ce02ee
TT
1056 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1057 gdbpy_enter enter_py (garch, current_language);
1058
88b6faea
TT
1059 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
1060 if (bp_obj != NULL)
d930d06e 1061 {
dac790e1
TT
1062 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
1063 {
88b6faea 1064 if (evpy_emit_event ((PyObject *) bp_obj.get (),
dac790e1
TT
1065 gdb_py_events.breakpoint_deleted) < 0)
1066 gdbpy_print_stack ();
1067 }
1068
d930d06e
TT
1069 bp_obj->bp = NULL;
1070 --bppy_live;
d930d06e 1071 }
adc36818 1072 }
adc36818
PM
1073}
1074
dac790e1
TT
1075/* Callback that is used when a breakpoint is modified. */
1076
1077static void
1078gdbpy_breakpoint_modified (struct breakpoint *b)
1079{
1080 int num = b->number;
dac790e1 1081 struct breakpoint *bp = NULL;
dac790e1 1082
dac790e1
TT
1083 bp = get_breakpoint (num);
1084 if (bp)
1085 {
25ce02ee
TT
1086 struct gdbarch *garch = bp->gdbarch ? bp->gdbarch : get_current_arch ();
1087 gdbpy_enter enter_py (garch, current_language);
1088
dac790e1
TT
1089 PyObject *bp_obj = (PyObject *) bp->py_bp_object;
1090 if (bp_obj)
1091 {
1092 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
1093 {
dac790e1
TT
1094 if (evpy_emit_event (bp_obj,
1095 gdb_py_events.breakpoint_modified) < 0)
1096 gdbpy_print_stack ();
1097 }
1098 }
1099 }
dac790e1
TT
1100}
1101
adc36818
PM
1102\f
1103
1104/* Initialize the Python breakpoint code. */
999633ed 1105int
adc36818
PM
1106gdbpy_initialize_breakpoints (void)
1107{
1108 int i;
1109
6a1b1664 1110 breakpoint_object_type.tp_new = PyType_GenericNew;
adc36818 1111 if (PyType_Ready (&breakpoint_object_type) < 0)
999633ed 1112 return -1;
adc36818 1113
aa36459a
TT
1114 if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
1115 (PyObject *) &breakpoint_object_type) < 0)
999633ed 1116 return -1;
adc36818 1117
76727919
TT
1118 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created);
1119 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted);
1120 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified);
adc36818
PM
1121
1122 /* Add breakpoint types constants. */
1123 for (i = 0; pybp_codes[i].name; ++i)
1124 {
6c28e44a 1125 if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
adc36818 1126 pybp_codes[i].code) < 0)
999633ed 1127 return -1;
adc36818
PM
1128 }
1129
1130 /* Add watchpoint types constants. */
1131 for (i = 0; pybp_watch_types[i].name; ++i)
1132 {
6c28e44a 1133 if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
adc36818 1134 pybp_watch_types[i].code) < 0)
999633ed 1135 return -1;
adc36818
PM
1136 }
1137
999633ed 1138 return 0;
adc36818
PM
1139}
1140
1141\f
1142
7371cf6d
PM
1143/* Helper function that overrides this Python object's
1144 PyObject_GenericSetAttr to allow extra validation of the attribute
1145 being set. */
1146
256458bc 1147static int
7371cf6d
PM
1148local_setattro (PyObject *self, PyObject *name, PyObject *v)
1149{
256458bc 1150 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
9b972014 1151 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
256458bc 1152
7371cf6d
PM
1153 if (attr == NULL)
1154 return -1;
256458bc 1155
7371cf6d 1156 /* If the attribute trying to be set is the "stop" method,
6dddc817
DE
1157 but we already have a condition set in the CLI or other extension
1158 language, disallow this operation. */
9b972014 1159 if (strcmp (attr.get (), stop_func) == 0)
7371cf6d 1160 {
6dddc817
DE
1161 const struct extension_language_defn *extlang = NULL;
1162
1163 if (obj->bp->cond_string != NULL)
1164 extlang = get_ext_lang_defn (EXT_LANG_GDB);
1165 if (extlang == NULL)
1166 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
1167 if (extlang != NULL)
1168 {
7f968c89
TT
1169 std::string error_text
1170 = string_printf (_("Only one stop condition allowed. There is"
1171 " currently a %s stop condition defined for"
1172 " this breakpoint."),
1173 ext_lang_capitalized_name (extlang));
1174 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
6dddc817
DE
1175 return -1;
1176 }
7371cf6d 1177 }
256458bc 1178
8833fbf0 1179 return PyObject_GenericSetAttr (self, name, v);
7371cf6d
PM
1180}
1181
0d1f4ceb 1182static gdb_PyGetSetDef breakpoint_object_getset[] = {
adc36818
PM
1183 { "enabled", bppy_get_enabled, bppy_set_enabled,
1184 "Boolean telling whether the breakpoint is enabled.", NULL },
1185 { "silent", bppy_get_silent, bppy_set_silent,
1186 "Boolean telling whether the breakpoint is silent.", NULL },
1187 { "thread", bppy_get_thread, bppy_set_thread,
1188 "Thread ID for the breakpoint.\n\
1189If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
1190If the value is None, then this breakpoint is not thread-specific.\n\
1191No other type of value can be used.", NULL },
1192 { "task", bppy_get_task, bppy_set_task,
1193 "Thread ID for the breakpoint.\n\
1194If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
1195If the value is None, then this breakpoint is not task-specific.\n\
1196No other type of value can be used.", NULL },
1197 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
1198 "Number of times this breakpoint should be automatically continued.",
1199 NULL },
1200 { "number", bppy_get_number, NULL,
1201 "Breakpoint's number assigned by GDB.", NULL },
1202 { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
1203 "Number of times the breakpoint has been hit.\n\
1204Can be set to zero to clear the count. No other value is valid\n\
1205when setting this property.", NULL },
1206 { "location", bppy_get_location, NULL,
1207 "Location of the breakpoint, as specified by the user.", NULL},
1208 { "expression", bppy_get_expression, NULL,
1209 "Expression of the breakpoint, as specified by the user.", NULL},
1210 { "condition", bppy_get_condition, bppy_set_condition,
1211 "Condition of the breakpoint, as specified by the user,\
1212or None if no condition set."},
a913fffb 1213 { "commands", bppy_get_commands, bppy_set_commands,
adc36818
PM
1214 "Commands of the breakpoint, as specified by the user."},
1215 { "type", bppy_get_type, NULL,
1216 "Type of breakpoint."},
84f4c1fe
PM
1217 { "visible", bppy_get_visibility, NULL,
1218 "Whether the breakpoint is visible to the user."},
f76c27b5
PM
1219 { "temporary", bppy_get_temporary, NULL,
1220 "Whether this breakpoint is a temporary breakpoint."},
93daf339
TT
1221 { "pending", bppy_get_pending, NULL,
1222 "Whether this breakpoint is a pending breakpoint."},
adc36818
PM
1223 { NULL } /* Sentinel. */
1224};
1225
1226static PyMethodDef breakpoint_object_methods[] =
1227{
1228 { "is_valid", bppy_is_valid, METH_NOARGS,
1229 "Return true if this breakpoint is valid, false if not." },
94b6973e
PM
1230 { "delete", bppy_delete_breakpoint, METH_NOARGS,
1231 "Delete the underlying GDB breakpoint." },
adc36818
PM
1232 { NULL } /* Sentinel. */
1233};
1234
cc72b2a2 1235PyTypeObject breakpoint_object_type =
adc36818 1236{
9a27f2c6 1237 PyVarObject_HEAD_INIT (NULL, 0)
adc36818 1238 "gdb.Breakpoint", /*tp_name*/
4cb0213d 1239 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
adc36818
PM
1240 0, /*tp_itemsize*/
1241 0, /*tp_dealloc*/
1242 0, /*tp_print*/
1243 0, /*tp_getattr*/
1244 0, /*tp_setattr*/
1245 0, /*tp_compare*/
1246 0, /*tp_repr*/
1247 0, /*tp_as_number*/
1248 0, /*tp_as_sequence*/
1249 0, /*tp_as_mapping*/
1250 0, /*tp_hash */
1251 0, /*tp_call*/
1252 0, /*tp_str*/
1253 0, /*tp_getattro*/
7371cf6d 1254 (setattrofunc)local_setattro, /*tp_setattro */
adc36818 1255 0, /*tp_as_buffer*/
7371cf6d 1256 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
adc36818
PM
1257 "GDB breakpoint object", /* tp_doc */
1258 0, /* tp_traverse */
1259 0, /* tp_clear */
1260 0, /* tp_richcompare */
1261 0, /* tp_weaklistoffset */
1262 0, /* tp_iter */
1263 0, /* tp_iternext */
1264 breakpoint_object_methods, /* tp_methods */
1265 0, /* tp_members */
7371cf6d
PM
1266 breakpoint_object_getset, /* tp_getset */
1267 0, /* tp_base */
1268 0, /* tp_dict */
1269 0, /* tp_descr_get */
1270 0, /* tp_descr_set */
1271 0, /* tp_dictoffset */
1272 bppy_init, /* tp_init */
1273 0, /* tp_alloc */
adc36818 1274};
This page took 0.994728 seconds and 4 git commands to generate.