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