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