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