]>
Commit | Line | Data |
---|---|---|
adc36818 PM |
1 | /* Python interface to breakpoints |
2 | ||
7b6bb8da | 3 | Copyright (C) 2008, 2009, 2010, 2011 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" | |
22 | #include "exceptions.h" | |
23 | #include "python-internal.h" | |
24 | #include "charset.h" | |
25 | #include "breakpoint.h" | |
26 | #include "gdbcmd.h" | |
27 | #include "gdbthread.h" | |
28 | #include "observer.h" | |
29 | #include "cli/cli-script.h" | |
30 | #include "ada-lang.h" | |
7371cf6d PM |
31 | #include "arch-utils.h" |
32 | #include "language.h" | |
adc36818 | 33 | |
adc36818 PM |
34 | static PyTypeObject breakpoint_object_type; |
35 | ||
adc36818 PM |
36 | /* Number of live breakpoints. */ |
37 | static int bppy_live; | |
38 | ||
39 | /* Variables used to pass information between the Breakpoint | |
40 | constructor and the breakpoint-created hook function. */ | |
41 | static breakpoint_object *bppy_pending_object; | |
42 | ||
7371cf6d PM |
43 | /* Function that is called when a Python condition is evaluated. */ |
44 | static char * const stop_func = "stop"; | |
45 | ||
adc36818 PM |
46 | struct breakpoint_object |
47 | { | |
48 | PyObject_HEAD | |
49 | ||
50 | /* The breakpoint number according to gdb. */ | |
51 | int number; | |
52 | ||
53 | /* The gdb breakpoint object, or NULL if the breakpoint has been | |
54 | deleted. */ | |
55 | struct breakpoint *bp; | |
56 | }; | |
57 | ||
58 | /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python | |
59 | exception if it is invalid. */ | |
60 | #define BPPY_REQUIRE_VALID(Breakpoint) \ | |
61 | do { \ | |
84f4c1fe | 62 | if ((Breakpoint)->bp == NULL) \ |
9a2b4c1b MS |
63 | return PyErr_Format (PyExc_RuntimeError, \ |
64 | _("Breakpoint %d is invalid."), \ | |
adc36818 PM |
65 | (Breakpoint)->number); \ |
66 | } while (0) | |
67 | ||
68 | /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python | |
69 | exception if it is invalid. This macro is for use in setter functions. */ | |
70 | #define BPPY_SET_REQUIRE_VALID(Breakpoint) \ | |
71 | do { \ | |
9a2b4c1b | 72 | if ((Breakpoint)->bp == NULL) \ |
adc36818 PM |
73 | { \ |
74 | PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \ | |
75 | (Breakpoint)->number); \ | |
76 | return -1; \ | |
77 | } \ | |
78 | } while (0) | |
79 | ||
80 | /* This is used to initialize various gdb.bp_* constants. */ | |
81 | struct pybp_code | |
82 | { | |
83 | /* The name. */ | |
84 | const char *name; | |
85 | /* The code. */ | |
86 | enum type_code code; | |
87 | }; | |
88 | ||
89 | /* Entries related to the type of user set breakpoints. */ | |
90 | static struct pybp_code pybp_codes[] = | |
91 | { | |
92 | { "BP_NONE", bp_none}, | |
93 | { "BP_BREAKPOINT", bp_breakpoint}, | |
94 | { "BP_WATCHPOINT", bp_watchpoint}, | |
95 | { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint}, | |
96 | { "BP_READ_WATCHPOINT", bp_read_watchpoint}, | |
97 | { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint}, | |
98 | {NULL} /* Sentinel. */ | |
99 | }; | |
100 | ||
101 | /* Entries related to the type of watchpoint. */ | |
102 | static struct pybp_code pybp_watch_types[] = | |
103 | { | |
104 | { "WP_READ", hw_read}, | |
105 | { "WP_WRITE", hw_write}, | |
106 | { "WP_ACCESS", hw_access}, | |
107 | {NULL} /* Sentinel. */ | |
108 | }; | |
109 | ||
adc36818 PM |
110 | /* Python function which checks the validity of a breakpoint object. */ |
111 | static PyObject * | |
112 | bppy_is_valid (PyObject *self, PyObject *args) | |
113 | { | |
114 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
115 | ||
116 | if (self_bp->bp) | |
117 | Py_RETURN_TRUE; | |
118 | Py_RETURN_FALSE; | |
119 | } | |
120 | ||
121 | /* Python function to test whether or not the breakpoint is enabled. */ | |
122 | static PyObject * | |
123 | bppy_get_enabled (PyObject *self, void *closure) | |
124 | { | |
125 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
126 | ||
127 | BPPY_REQUIRE_VALID (self_bp); | |
128 | if (! self_bp->bp) | |
129 | Py_RETURN_FALSE; | |
130 | if (self_bp->bp->enable_state == bp_enabled) | |
131 | Py_RETURN_TRUE; | |
132 | Py_RETURN_FALSE; | |
133 | } | |
134 | ||
135 | /* Python function to test whether or not the breakpoint is silent. */ | |
136 | static PyObject * | |
137 | bppy_get_silent (PyObject *self, void *closure) | |
138 | { | |
139 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
140 | ||
141 | BPPY_REQUIRE_VALID (self_bp); | |
142 | if (self_bp->bp->silent) | |
143 | Py_RETURN_TRUE; | |
144 | Py_RETURN_FALSE; | |
145 | } | |
146 | ||
147 | /* Python function to set the enabled state of a breakpoint. */ | |
148 | static int | |
149 | bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure) | |
150 | { | |
151 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
152 | int cmp; | |
76dce0be | 153 | volatile struct gdb_exception except; |
adc36818 PM |
154 | |
155 | BPPY_SET_REQUIRE_VALID (self_bp); | |
156 | ||
157 | if (newvalue == NULL) | |
158 | { | |
159 | PyErr_SetString (PyExc_TypeError, | |
160 | _("Cannot delete `enabled' attribute.")); | |
161 | ||
162 | return -1; | |
163 | } | |
164 | else if (! PyBool_Check (newvalue)) | |
165 | { | |
166 | PyErr_SetString (PyExc_TypeError, | |
167 | _("The value of `enabled' must be a boolean.")); | |
168 | return -1; | |
169 | } | |
170 | ||
171 | cmp = PyObject_IsTrue (newvalue); | |
172 | if (cmp < 0) | |
173 | return -1; | |
76dce0be PM |
174 | |
175 | TRY_CATCH (except, RETURN_MASK_ALL) | |
176 | { | |
177 | if (cmp == 1) | |
178 | enable_breakpoint (self_bp->bp); | |
179 | else | |
180 | disable_breakpoint (self_bp->bp); | |
181 | } | |
182 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
183 | ||
adc36818 PM |
184 | return 0; |
185 | } | |
186 | ||
187 | /* Python function to set the 'silent' state of a breakpoint. */ | |
188 | static int | |
189 | bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure) | |
190 | { | |
191 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
192 | int cmp; | |
193 | ||
194 | BPPY_SET_REQUIRE_VALID (self_bp); | |
195 | ||
196 | if (newvalue == NULL) | |
197 | { | |
198 | PyErr_SetString (PyExc_TypeError, | |
199 | _("Cannot delete `silent' attribute.")); | |
200 | return -1; | |
201 | } | |
202 | else if (! PyBool_Check (newvalue)) | |
203 | { | |
204 | PyErr_SetString (PyExc_TypeError, | |
205 | _("The value of `silent' must be a boolean.")); | |
206 | return -1; | |
207 | } | |
208 | ||
209 | cmp = PyObject_IsTrue (newvalue); | |
210 | if (cmp < 0) | |
211 | return -1; | |
212 | else | |
45a43567 | 213 | breakpoint_set_silent (self_bp->bp, cmp); |
adc36818 PM |
214 | |
215 | return 0; | |
216 | } | |
217 | ||
218 | /* Python function to set the thread of a breakpoint. */ | |
219 | static int | |
220 | bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure) | |
221 | { | |
222 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
74aedc46 | 223 | long id; |
adc36818 PM |
224 | |
225 | BPPY_SET_REQUIRE_VALID (self_bp); | |
226 | ||
227 | if (newvalue == NULL) | |
228 | { | |
229 | PyErr_SetString (PyExc_TypeError, | |
230 | _("Cannot delete `thread' attribute.")); | |
231 | return -1; | |
232 | } | |
233 | else if (PyInt_Check (newvalue)) | |
234 | { | |
74aedc46 TT |
235 | if (! gdb_py_int_as_long (newvalue, &id)) |
236 | return -1; | |
237 | ||
adc36818 PM |
238 | if (! valid_thread_id (id)) |
239 | { | |
240 | PyErr_SetString (PyExc_RuntimeError, | |
241 | _("Invalid thread ID.")); | |
242 | return -1; | |
243 | } | |
244 | } | |
245 | else if (newvalue == Py_None) | |
246 | id = -1; | |
247 | else | |
248 | { | |
249 | PyErr_SetString (PyExc_TypeError, | |
250 | _("The value of `thread' must be an integer or None.")); | |
251 | return -1; | |
252 | } | |
253 | ||
45a43567 | 254 | breakpoint_set_thread (self_bp->bp, id); |
adc36818 PM |
255 | |
256 | return 0; | |
257 | } | |
258 | ||
259 | /* Python function to set the (Ada) task of a breakpoint. */ | |
260 | static int | |
261 | bppy_set_task (PyObject *self, PyObject *newvalue, void *closure) | |
262 | { | |
263 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
74aedc46 | 264 | long id; |
76dce0be PM |
265 | int valid_id = 0; |
266 | volatile struct gdb_exception except; | |
adc36818 PM |
267 | |
268 | BPPY_SET_REQUIRE_VALID (self_bp); | |
269 | ||
270 | if (newvalue == NULL) | |
271 | { | |
272 | PyErr_SetString (PyExc_TypeError, | |
273 | _("Cannot delete `task' attribute.")); | |
274 | return -1; | |
275 | } | |
276 | else if (PyInt_Check (newvalue)) | |
277 | { | |
74aedc46 TT |
278 | if (! gdb_py_int_as_long (newvalue, &id)) |
279 | return -1; | |
280 | ||
76dce0be PM |
281 | TRY_CATCH (except, RETURN_MASK_ALL) |
282 | { | |
283 | valid_id = valid_task_id (id); | |
284 | } | |
285 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
286 | ||
287 | if (! valid_id) | |
adc36818 PM |
288 | { |
289 | PyErr_SetString (PyExc_RuntimeError, | |
290 | _("Invalid task ID.")); | |
291 | return -1; | |
292 | } | |
293 | } | |
294 | else if (newvalue == Py_None) | |
295 | id = 0; | |
296 | else | |
297 | { | |
298 | PyErr_SetString (PyExc_TypeError, | |
299 | _("The value of `task' must be an integer or None.")); | |
300 | return -1; | |
301 | } | |
302 | ||
45a43567 | 303 | breakpoint_set_task (self_bp->bp, id); |
adc36818 PM |
304 | |
305 | return 0; | |
306 | } | |
307 | ||
94b6973e PM |
308 | /* Python function which deletes the underlying GDB breakpoint. This |
309 | triggers the breakpoint_deleted observer which will call | |
310 | gdbpy_breakpoint_deleted; that function cleans up the Python | |
311 | sections. */ | |
312 | ||
313 | static PyObject * | |
314 | bppy_delete_breakpoint (PyObject *self, PyObject *args) | |
315 | { | |
316 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
76dce0be | 317 | volatile struct gdb_exception except; |
94b6973e PM |
318 | |
319 | BPPY_REQUIRE_VALID (self_bp); | |
320 | ||
76dce0be PM |
321 | TRY_CATCH (except, RETURN_MASK_ALL) |
322 | { | |
323 | delete_breakpoint (self_bp->bp); | |
324 | } | |
325 | GDB_PY_HANDLE_EXCEPTION (except); | |
94b6973e PM |
326 | |
327 | Py_RETURN_NONE; | |
328 | } | |
329 | ||
adc36818 PM |
330 | |
331 | /* Python function to set the ignore count of a breakpoint. */ | |
332 | static int | |
333 | bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure) | |
334 | { | |
335 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
336 | long value; | |
337 | ||
338 | BPPY_SET_REQUIRE_VALID (self_bp); | |
339 | ||
340 | if (newvalue == NULL) | |
341 | { | |
342 | PyErr_SetString (PyExc_TypeError, | |
343 | _("Cannot delete `ignore_count' attribute.")); | |
344 | return -1; | |
345 | } | |
346 | else if (! PyInt_Check (newvalue)) | |
347 | { | |
348 | PyErr_SetString (PyExc_TypeError, | |
349 | _("The value of `ignore_count' must be an integer.")); | |
350 | return -1; | |
351 | } | |
352 | ||
74aedc46 TT |
353 | if (! gdb_py_int_as_long (newvalue, &value)) |
354 | return -1; | |
355 | ||
adc36818 PM |
356 | if (value < 0) |
357 | value = 0; | |
358 | set_ignore_count (self_bp->number, (int) value, 0); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | /* Python function to set the hit count of a breakpoint. */ | |
364 | static int | |
365 | bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure) | |
366 | { | |
367 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
368 | ||
369 | BPPY_SET_REQUIRE_VALID (self_bp); | |
370 | ||
371 | if (newvalue == NULL) | |
372 | { | |
373 | PyErr_SetString (PyExc_TypeError, | |
374 | _("Cannot delete `hit_count' attribute.")); | |
375 | return -1; | |
376 | } | |
74aedc46 | 377 | else |
adc36818 | 378 | { |
74aedc46 TT |
379 | long value; |
380 | ||
381 | if (! gdb_py_int_as_long (newvalue, &value)) | |
382 | return -1; | |
383 | ||
384 | if (value != 0) | |
385 | { | |
386 | PyErr_SetString (PyExc_AttributeError, | |
387 | _("The value of `hit_count' must be zero.")); | |
388 | return -1; | |
389 | } | |
adc36818 PM |
390 | } |
391 | ||
392 | self_bp->bp->hit_count = 0; | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | /* Python function to get the location of a breakpoint. */ | |
398 | static PyObject * | |
399 | bppy_get_location (PyObject *self, void *closure) | |
400 | { | |
401 | char *str; | |
402 | breakpoint_object *obj = (breakpoint_object *) self; | |
403 | ||
404 | BPPY_REQUIRE_VALID (obj); | |
405 | ||
406 | if (obj->bp->type != bp_breakpoint) | |
407 | Py_RETURN_NONE; | |
408 | ||
409 | str = obj->bp->addr_string; | |
410 | ||
411 | if (! str) | |
412 | str = ""; | |
413 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
414 | } | |
415 | ||
416 | /* Python function to get the breakpoint expression. */ | |
417 | static PyObject * | |
418 | bppy_get_expression (PyObject *self, void *closure) | |
419 | { | |
420 | char *str; | |
421 | breakpoint_object *obj = (breakpoint_object *) self; | |
3a5c3e22 | 422 | struct watchpoint *wp; |
adc36818 PM |
423 | |
424 | BPPY_REQUIRE_VALID (obj); | |
425 | ||
3a5c3e22 | 426 | if (!is_watchpoint (obj->bp)) |
adc36818 PM |
427 | Py_RETURN_NONE; |
428 | ||
3a5c3e22 PA |
429 | wp = (struct watchpoint *) obj->bp; |
430 | ||
431 | str = wp->exp_string; | |
adc36818 PM |
432 | if (! str) |
433 | str = ""; | |
434 | ||
435 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
436 | } | |
437 | ||
438 | /* Python function to get the condition expression of a breakpoint. */ | |
439 | static PyObject * | |
440 | bppy_get_condition (PyObject *self, void *closure) | |
441 | { | |
442 | char *str; | |
443 | breakpoint_object *obj = (breakpoint_object *) self; | |
444 | ||
445 | BPPY_REQUIRE_VALID (obj); | |
446 | ||
447 | str = obj->bp->cond_string; | |
448 | if (! str) | |
449 | Py_RETURN_NONE; | |
450 | ||
451 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
452 | } | |
453 | ||
8dc78533 JK |
454 | /* Returns 0 on success. Returns -1 on error, with a python exception set. |
455 | */ | |
456 | ||
adc36818 PM |
457 | static int |
458 | bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure) | |
459 | { | |
460 | char *exp; | |
461 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
462 | volatile struct gdb_exception except; | |
463 | ||
464 | BPPY_SET_REQUIRE_VALID (self_bp); | |
465 | ||
466 | if (newvalue == NULL) | |
467 | { | |
468 | PyErr_SetString (PyExc_TypeError, | |
469 | _("Cannot delete `condition' attribute.")); | |
470 | return -1; | |
471 | } | |
472 | else if (newvalue == Py_None) | |
473 | exp = ""; | |
474 | else | |
475 | { | |
476 | exp = python_string_to_host_string (newvalue); | |
477 | if (exp == NULL) | |
478 | return -1; | |
479 | } | |
480 | ||
481 | TRY_CATCH (except, RETURN_MASK_ALL) | |
482 | { | |
483 | set_breakpoint_condition (self_bp->bp, exp, 0); | |
484 | } | |
f3be5b64 MS |
485 | |
486 | if (newvalue != Py_None) | |
487 | xfree (exp); | |
488 | ||
adc36818 PM |
489 | GDB_PY_SET_HANDLE_EXCEPTION (except); |
490 | ||
491 | return 0; | |
492 | } | |
493 | ||
494 | /* Python function to get the commands attached to a breakpoint. */ | |
495 | static PyObject * | |
496 | bppy_get_commands (PyObject *self, void *closure) | |
497 | { | |
498 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
499 | struct breakpoint *bp = self_bp->bp; | |
500 | long length; | |
501 | volatile struct gdb_exception except; | |
502 | struct ui_file *string_file; | |
503 | struct cleanup *chain; | |
504 | PyObject *result; | |
505 | char *cmdstr; | |
506 | ||
507 | BPPY_REQUIRE_VALID (self_bp); | |
508 | ||
509 | if (! self_bp->bp->commands) | |
510 | Py_RETURN_NONE; | |
511 | ||
512 | string_file = mem_fileopen (); | |
513 | chain = make_cleanup_ui_file_delete (string_file); | |
514 | ||
79a45e25 | 515 | ui_out_redirect (current_uiout, string_file); |
adc36818 PM |
516 | TRY_CATCH (except, RETURN_MASK_ALL) |
517 | { | |
79a45e25 | 518 | print_command_lines (current_uiout, breakpoint_commands (bp), 0); |
adc36818 | 519 | } |
79a45e25 | 520 | ui_out_redirect (current_uiout, NULL); |
adc36818 PM |
521 | GDB_PY_HANDLE_EXCEPTION (except); |
522 | ||
4c2d5724 MS |
523 | cmdstr = ui_file_xstrdup (string_file, &length); |
524 | make_cleanup (xfree, cmdstr); | |
adc36818 PM |
525 | result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL); |
526 | do_cleanups (chain); | |
adc36818 PM |
527 | return result; |
528 | } | |
529 | ||
530 | /* Python function to get the breakpoint type. */ | |
531 | static PyObject * | |
532 | bppy_get_type (PyObject *self, void *closure) | |
533 | { | |
534 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
535 | ||
536 | BPPY_REQUIRE_VALID (self_bp); | |
537 | ||
538 | return PyInt_FromLong (self_bp->bp->type); | |
539 | } | |
540 | ||
84f4c1fe PM |
541 | /* Python function to get the visibility of the breakpoint. */ |
542 | ||
543 | static PyObject * | |
544 | bppy_get_visibility (PyObject *self, void *closure) | |
545 | { | |
546 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
547 | ||
548 | BPPY_REQUIRE_VALID (self_bp); | |
549 | ||
550 | if (self_bp->bp->number < 0) | |
551 | Py_RETURN_FALSE; | |
552 | ||
553 | Py_RETURN_TRUE; | |
554 | } | |
555 | ||
adc36818 PM |
556 | /* Python function to get the breakpoint's number. */ |
557 | static PyObject * | |
558 | bppy_get_number (PyObject *self, void *closure) | |
559 | { | |
560 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
561 | ||
562 | BPPY_REQUIRE_VALID (self_bp); | |
563 | ||
564 | return PyInt_FromLong (self_bp->number); | |
565 | } | |
566 | ||
567 | /* Python function to get the breakpoint's thread ID. */ | |
568 | static PyObject * | |
569 | bppy_get_thread (PyObject *self, void *closure) | |
570 | { | |
571 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
572 | ||
573 | BPPY_REQUIRE_VALID (self_bp); | |
574 | ||
575 | if (self_bp->bp->thread == -1) | |
576 | Py_RETURN_NONE; | |
577 | ||
578 | return PyInt_FromLong (self_bp->bp->thread); | |
579 | } | |
580 | ||
581 | /* Python function to get the breakpoint's task ID (in Ada). */ | |
582 | static PyObject * | |
583 | bppy_get_task (PyObject *self, void *closure) | |
584 | { | |
585 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
586 | ||
587 | BPPY_REQUIRE_VALID (self_bp); | |
588 | ||
589 | if (self_bp->bp->task == 0) | |
590 | Py_RETURN_NONE; | |
591 | ||
592 | return PyInt_FromLong (self_bp->bp->task); | |
593 | } | |
594 | ||
595 | /* Python function to get the breakpoint's hit count. */ | |
596 | static PyObject * | |
597 | bppy_get_hit_count (PyObject *self, void *closure) | |
598 | { | |
599 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
600 | ||
601 | BPPY_REQUIRE_VALID (self_bp); | |
602 | ||
603 | return PyInt_FromLong (self_bp->bp->hit_count); | |
604 | } | |
605 | ||
606 | /* Python function to get the breakpoint's ignore count. */ | |
607 | static PyObject * | |
608 | bppy_get_ignore_count (PyObject *self, void *closure) | |
609 | { | |
610 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
611 | ||
612 | BPPY_REQUIRE_VALID (self_bp); | |
613 | ||
614 | return PyInt_FromLong (self_bp->bp->ignore_count); | |
615 | } | |
616 | ||
617 | /* Python function to create a new breakpoint. */ | |
7371cf6d PM |
618 | static int |
619 | bppy_init (PyObject *self, PyObject *args, PyObject *kwargs) | |
adc36818 | 620 | { |
84f4c1fe | 621 | static char *keywords[] = { "spec", "type", "wp_class", "internal", NULL }; |
ddd49eee | 622 | const char *spec; |
adc36818 PM |
623 | int type = bp_breakpoint; |
624 | int access_type = hw_write; | |
84f4c1fe PM |
625 | PyObject *internal = NULL; |
626 | int internal_bp = 0; | |
adc36818 PM |
627 | volatile struct gdb_exception except; |
628 | ||
84f4c1fe PM |
629 | if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiO", keywords, |
630 | &spec, &type, &access_type, &internal)) | |
7371cf6d | 631 | return -1; |
adc36818 | 632 | |
84f4c1fe PM |
633 | if (internal) |
634 | { | |
635 | internal_bp = PyObject_IsTrue (internal); | |
636 | if (internal_bp == -1) | |
7371cf6d | 637 | return -1; |
84f4c1fe PM |
638 | } |
639 | ||
7371cf6d | 640 | bppy_pending_object = (breakpoint_object *) self; |
adc36818 PM |
641 | bppy_pending_object->number = -1; |
642 | bppy_pending_object->bp = NULL; | |
643 | ||
644 | TRY_CATCH (except, RETURN_MASK_ALL) | |
645 | { | |
ddd49eee TT |
646 | char *copy = xstrdup (spec); |
647 | struct cleanup *cleanup = make_cleanup (xfree, copy); | |
648 | ||
adc36818 PM |
649 | switch (type) |
650 | { | |
651 | case bp_breakpoint: | |
652 | { | |
653 | create_breakpoint (python_gdbarch, | |
ddd49eee | 654 | copy, NULL, -1, |
adc36818 | 655 | 0, |
0fb4aa4b | 656 | 0, bp_breakpoint, |
adc36818 PM |
657 | 0, |
658 | AUTO_BOOLEAN_TRUE, | |
348d480f PA |
659 | &bkpt_breakpoint_ops, |
660 | 0, 1, internal_bp); | |
adc36818 PM |
661 | break; |
662 | } | |
663 | case bp_watchpoint: | |
664 | { | |
665 | if (access_type == hw_write) | |
ddd49eee | 666 | watch_command_wrapper (copy, 0, internal_bp); |
adc36818 | 667 | else if (access_type == hw_access) |
ddd49eee | 668 | awatch_command_wrapper (copy, 0, internal_bp); |
adc36818 | 669 | else if (access_type == hw_read) |
ddd49eee | 670 | rwatch_command_wrapper (copy, 0, internal_bp); |
adc36818 PM |
671 | else |
672 | error(_("Cannot understand watchpoint access type.")); | |
673 | break; | |
674 | } | |
675 | default: | |
676 | error(_("Do not understand breakpoint type to set.")); | |
677 | } | |
ddd49eee TT |
678 | |
679 | do_cleanups (cleanup); | |
adc36818 PM |
680 | } |
681 | if (except.reason < 0) | |
682 | { | |
7371cf6d PM |
683 | PyErr_Format (except.reason == RETURN_QUIT |
684 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
685 | "%s", except.message); | |
686 | return -1; | |
adc36818 PM |
687 | } |
688 | ||
7371cf6d PM |
689 | BPPY_SET_REQUIRE_VALID ((breakpoint_object *) self); |
690 | return 0; | |
adc36818 PM |
691 | } |
692 | ||
693 | \f | |
694 | ||
84f4c1fe PM |
695 | static int |
696 | build_bp_list (struct breakpoint *b, void *arg) | |
697 | { | |
698 | PyObject *list = arg; | |
50389644 | 699 | PyObject *bp = (PyObject *) b->py_bp_object; |
84f4c1fe PM |
700 | int iserr = 0; |
701 | ||
702 | /* Not all breakpoints will have a companion Python object. | |
703 | Only breakpoints that were created via bppy_new, or | |
704 | breakpoints that were created externally and are tracked by | |
705 | the Python Scripting API. */ | |
706 | if (bp) | |
707 | iserr = PyList_Append (list, bp); | |
708 | ||
709 | if (iserr == -1) | |
710 | return 1; | |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
adc36818 PM |
715 | /* Static function to return a tuple holding all breakpoints. */ |
716 | ||
717 | PyObject * | |
718 | gdbpy_breakpoints (PyObject *self, PyObject *args) | |
719 | { | |
27ca1a5b | 720 | PyObject *list, *tuple; |
adc36818 PM |
721 | |
722 | if (bppy_live == 0) | |
723 | Py_RETURN_NONE; | |
724 | ||
84f4c1fe PM |
725 | list = PyList_New (0); |
726 | if (!list) | |
727 | return NULL; | |
d59b6f6c | 728 | |
84f4c1fe PM |
729 | /* If iteratre_over_breakpoints returns non NULL it signals an error |
730 | condition. In that case abandon building the list and return | |
731 | NULL. */ | |
732 | if (iterate_over_breakpoints (build_bp_list, list) != NULL) | |
733 | { | |
734 | Py_DECREF (list); | |
735 | return NULL; | |
adc36818 | 736 | } |
84f4c1fe | 737 | |
27ca1a5b PM |
738 | tuple = PyList_AsTuple (list); |
739 | Py_DECREF (list); | |
740 | ||
741 | return tuple; | |
adc36818 PM |
742 | } |
743 | ||
7371cf6d PM |
744 | /* Call the "stop" method (if implemented) in the breakpoint |
745 | class. If the method returns True, the inferior will be | |
746 | stopped at the breakpoint. Otherwise the inferior will be | |
747 | allowed to continue. */ | |
748 | ||
749 | int | |
750 | gdbpy_should_stop (struct breakpoint_object *bp_obj) | |
751 | { | |
752 | int stop = 1; | |
753 | ||
754 | PyObject *py_bp = (PyObject *) bp_obj; | |
755 | struct breakpoint *b = bp_obj->bp; | |
756 | struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch (); | |
757 | struct cleanup *cleanup = ensure_python_env (garch, current_language); | |
758 | ||
759 | if (PyObject_HasAttrString (py_bp, stop_func)) | |
760 | { | |
761 | PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL); | |
762 | ||
763 | if (result) | |
764 | { | |
765 | int evaluate = PyObject_IsTrue (result); | |
766 | ||
767 | if (evaluate == -1) | |
768 | gdbpy_print_stack (); | |
769 | ||
770 | /* If the "stop" function returns False that means | |
771 | the Python breakpoint wants GDB to continue. */ | |
772 | if (! evaluate) | |
773 | stop = 0; | |
774 | ||
775 | Py_DECREF (result); | |
776 | } | |
777 | else | |
778 | gdbpy_print_stack (); | |
779 | } | |
780 | do_cleanups (cleanup); | |
781 | ||
782 | return stop; | |
783 | } | |
784 | ||
785 | /* Checks if the "stop" method exists in this breakpoint. | |
786 | Used by condition_command to ensure mutual exclusion of breakpoint | |
787 | conditions. */ | |
788 | ||
789 | int | |
790 | gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj) | |
791 | { | |
792 | int has_func = 0; | |
793 | PyObject *py_bp = (PyObject *) bp_obj; | |
794 | struct gdbarch *garch = bp_obj->bp->gdbarch ? bp_obj->bp->gdbarch : | |
795 | get_current_arch (); | |
796 | struct cleanup *cleanup = ensure_python_env (garch, current_language); | |
797 | ||
5fe41fbf TT |
798 | if (py_bp != NULL) |
799 | has_func = PyObject_HasAttrString (py_bp, stop_func); | |
7371cf6d | 800 | |
7371cf6d PM |
801 | do_cleanups (cleanup); |
802 | ||
803 | return has_func; | |
804 | } | |
805 | ||
adc36818 PM |
806 | \f |
807 | ||
808 | /* Event callback functions. */ | |
809 | ||
810 | /* Callback that is used when a breakpoint is created. This function | |
811 | will create a new Python breakpoint object. */ | |
812 | static void | |
8d3788bd | 813 | gdbpy_breakpoint_created (struct breakpoint *bp) |
adc36818 PM |
814 | { |
815 | breakpoint_object *newbp; | |
adc36818 PM |
816 | PyGILState_STATE state; |
817 | ||
f6d90398 | 818 | if (bp->number < 0 && bppy_pending_object == NULL) |
84f4c1fe PM |
819 | return; |
820 | ||
adc36818 PM |
821 | if (bp->type != bp_breakpoint |
822 | && bp->type != bp_watchpoint | |
823 | && bp->type != bp_hardware_watchpoint | |
824 | && bp->type != bp_read_watchpoint | |
825 | && bp->type != bp_access_watchpoint) | |
826 | return; | |
827 | ||
adc36818 PM |
828 | state = PyGILState_Ensure (); |
829 | ||
830 | if (bppy_pending_object) | |
831 | { | |
832 | newbp = bppy_pending_object; | |
833 | bppy_pending_object = NULL; | |
834 | } | |
835 | else | |
836 | newbp = PyObject_New (breakpoint_object, &breakpoint_object_type); | |
837 | if (newbp) | |
838 | { | |
8d3788bd | 839 | newbp->number = bp->number; |
adc36818 | 840 | newbp->bp = bp; |
50389644 | 841 | newbp->bp->py_bp_object = newbp; |
adc36818 | 842 | Py_INCREF (newbp); |
84f4c1fe PM |
843 | ++bppy_live; |
844 | } | |
845 | else | |
846 | { | |
847 | PyErr_SetString (PyExc_RuntimeError, | |
848 | _("Error while creating breakpoint from GDB.")); | |
849 | gdbpy_print_stack (); | |
adc36818 | 850 | } |
adc36818 PM |
851 | |
852 | PyGILState_Release (state); | |
853 | } | |
854 | ||
855 | /* Callback that is used when a breakpoint is deleted. This will | |
856 | invalidate the corresponding Python object. */ | |
857 | static void | |
8d3788bd | 858 | gdbpy_breakpoint_deleted (struct breakpoint *b) |
adc36818 | 859 | { |
8d3788bd | 860 | int num = b->number; |
adc36818 | 861 | PyGILState_STATE state; |
84f4c1fe PM |
862 | struct breakpoint *bp = NULL; |
863 | breakpoint_object *bp_obj; | |
adc36818 PM |
864 | |
865 | state = PyGILState_Ensure (); | |
84f4c1fe | 866 | bp = get_breakpoint (num); |
d930d06e | 867 | if (bp) |
adc36818 | 868 | { |
d930d06e TT |
869 | bp_obj = bp->py_bp_object; |
870 | if (bp_obj) | |
871 | { | |
872 | bp_obj->bp = NULL; | |
873 | --bppy_live; | |
874 | Py_DECREF (bp_obj); | |
875 | } | |
adc36818 PM |
876 | } |
877 | PyGILState_Release (state); | |
878 | } | |
879 | ||
880 | \f | |
881 | ||
882 | /* Initialize the Python breakpoint code. */ | |
883 | void | |
884 | gdbpy_initialize_breakpoints (void) | |
885 | { | |
886 | int i; | |
887 | ||
6a1b1664 | 888 | breakpoint_object_type.tp_new = PyType_GenericNew; |
adc36818 PM |
889 | if (PyType_Ready (&breakpoint_object_type) < 0) |
890 | return; | |
891 | ||
892 | Py_INCREF (&breakpoint_object_type); | |
893 | PyModule_AddObject (gdb_module, "Breakpoint", | |
894 | (PyObject *) &breakpoint_object_type); | |
895 | ||
896 | observer_attach_breakpoint_created (gdbpy_breakpoint_created); | |
897 | observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted); | |
898 | ||
899 | /* Add breakpoint types constants. */ | |
900 | for (i = 0; pybp_codes[i].name; ++i) | |
901 | { | |
902 | if (PyModule_AddIntConstant (gdb_module, | |
903 | /* Cast needed for Python 2.4. */ | |
904 | (char *) pybp_codes[i].name, | |
905 | pybp_codes[i].code) < 0) | |
906 | return; | |
907 | } | |
908 | ||
909 | /* Add watchpoint types constants. */ | |
910 | for (i = 0; pybp_watch_types[i].name; ++i) | |
911 | { | |
912 | if (PyModule_AddIntConstant (gdb_module, | |
913 | /* Cast needed for Python 2.4. */ | |
914 | (char *) pybp_watch_types[i].name, | |
915 | pybp_watch_types[i].code) < 0) | |
916 | return; | |
917 | } | |
918 | ||
919 | } | |
920 | ||
921 | \f | |
922 | ||
7371cf6d PM |
923 | /* Helper function that overrides this Python object's |
924 | PyObject_GenericSetAttr to allow extra validation of the attribute | |
925 | being set. */ | |
926 | ||
927 | static int | |
928 | local_setattro (PyObject *self, PyObject *name, PyObject *v) | |
929 | { | |
930 | breakpoint_object *obj = (breakpoint_object *) self; | |
931 | char *attr = python_string_to_host_string (name); | |
932 | ||
933 | if (attr == NULL) | |
934 | return -1; | |
935 | ||
936 | /* If the attribute trying to be set is the "stop" method, | |
937 | but we already have a condition set in the CLI, disallow this | |
938 | operation. */ | |
939 | if (strcmp (attr, stop_func) == 0 && obj->bp->cond_string) | |
940 | { | |
941 | xfree (attr); | |
942 | PyErr_SetString (PyExc_RuntimeError, | |
943 | _("Cannot set 'stop' method. There is an " \ | |
944 | "existing GDB condition attached to the " \ | |
945 | "breakpoint.")); | |
946 | return -1; | |
947 | } | |
948 | ||
949 | xfree (attr); | |
950 | ||
951 | return PyObject_GenericSetAttr ((PyObject *)self, name, v); | |
952 | } | |
953 | ||
adc36818 PM |
954 | static PyGetSetDef breakpoint_object_getset[] = { |
955 | { "enabled", bppy_get_enabled, bppy_set_enabled, | |
956 | "Boolean telling whether the breakpoint is enabled.", NULL }, | |
957 | { "silent", bppy_get_silent, bppy_set_silent, | |
958 | "Boolean telling whether the breakpoint is silent.", NULL }, | |
959 | { "thread", bppy_get_thread, bppy_set_thread, | |
960 | "Thread ID for the breakpoint.\n\ | |
961 | If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\ | |
962 | If the value is None, then this breakpoint is not thread-specific.\n\ | |
963 | No other type of value can be used.", NULL }, | |
964 | { "task", bppy_get_task, bppy_set_task, | |
965 | "Thread ID for the breakpoint.\n\ | |
966 | If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\ | |
967 | If the value is None, then this breakpoint is not task-specific.\n\ | |
968 | No other type of value can be used.", NULL }, | |
969 | { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count, | |
970 | "Number of times this breakpoint should be automatically continued.", | |
971 | NULL }, | |
972 | { "number", bppy_get_number, NULL, | |
973 | "Breakpoint's number assigned by GDB.", NULL }, | |
974 | { "hit_count", bppy_get_hit_count, bppy_set_hit_count, | |
975 | "Number of times the breakpoint has been hit.\n\ | |
976 | Can be set to zero to clear the count. No other value is valid\n\ | |
977 | when setting this property.", NULL }, | |
978 | { "location", bppy_get_location, NULL, | |
979 | "Location of the breakpoint, as specified by the user.", NULL}, | |
980 | { "expression", bppy_get_expression, NULL, | |
981 | "Expression of the breakpoint, as specified by the user.", NULL}, | |
982 | { "condition", bppy_get_condition, bppy_set_condition, | |
983 | "Condition of the breakpoint, as specified by the user,\ | |
984 | or None if no condition set."}, | |
985 | { "commands", bppy_get_commands, NULL, | |
986 | "Commands of the breakpoint, as specified by the user."}, | |
987 | { "type", bppy_get_type, NULL, | |
988 | "Type of breakpoint."}, | |
84f4c1fe PM |
989 | { "visible", bppy_get_visibility, NULL, |
990 | "Whether the breakpoint is visible to the user."}, | |
adc36818 PM |
991 | { NULL } /* Sentinel. */ |
992 | }; | |
993 | ||
994 | static PyMethodDef breakpoint_object_methods[] = | |
995 | { | |
996 | { "is_valid", bppy_is_valid, METH_NOARGS, | |
997 | "Return true if this breakpoint is valid, false if not." }, | |
94b6973e PM |
998 | { "delete", bppy_delete_breakpoint, METH_NOARGS, |
999 | "Delete the underlying GDB breakpoint." }, | |
adc36818 PM |
1000 | { NULL } /* Sentinel. */ |
1001 | }; | |
1002 | ||
1003 | static PyTypeObject breakpoint_object_type = | |
1004 | { | |
1005 | PyObject_HEAD_INIT (NULL) | |
1006 | 0, /*ob_size*/ | |
1007 | "gdb.Breakpoint", /*tp_name*/ | |
1008 | sizeof (breakpoint_object), /*tp_basicsize*/ | |
1009 | 0, /*tp_itemsize*/ | |
1010 | 0, /*tp_dealloc*/ | |
1011 | 0, /*tp_print*/ | |
1012 | 0, /*tp_getattr*/ | |
1013 | 0, /*tp_setattr*/ | |
1014 | 0, /*tp_compare*/ | |
1015 | 0, /*tp_repr*/ | |
1016 | 0, /*tp_as_number*/ | |
1017 | 0, /*tp_as_sequence*/ | |
1018 | 0, /*tp_as_mapping*/ | |
1019 | 0, /*tp_hash */ | |
1020 | 0, /*tp_call*/ | |
1021 | 0, /*tp_str*/ | |
1022 | 0, /*tp_getattro*/ | |
7371cf6d | 1023 | (setattrofunc)local_setattro, /*tp_setattro */ |
adc36818 | 1024 | 0, /*tp_as_buffer*/ |
7371cf6d | 1025 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
adc36818 PM |
1026 | "GDB breakpoint object", /* tp_doc */ |
1027 | 0, /* tp_traverse */ | |
1028 | 0, /* tp_clear */ | |
1029 | 0, /* tp_richcompare */ | |
1030 | 0, /* tp_weaklistoffset */ | |
1031 | 0, /* tp_iter */ | |
1032 | 0, /* tp_iternext */ | |
1033 | breakpoint_object_methods, /* tp_methods */ | |
1034 | 0, /* tp_members */ | |
7371cf6d PM |
1035 | breakpoint_object_getset, /* tp_getset */ |
1036 | 0, /* tp_base */ | |
1037 | 0, /* tp_dict */ | |
1038 | 0, /* tp_descr_get */ | |
1039 | 0, /* tp_descr_set */ | |
1040 | 0, /* tp_dictoffset */ | |
1041 | bppy_init, /* tp_init */ | |
1042 | 0, /* tp_alloc */ | |
adc36818 | 1043 | }; |