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