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