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