Commit | Line | Data |
---|---|---|
adc36818 PM |
1 | /* Python interface to breakpoints |
2 | ||
3 | Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc. | |
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 | ||
286 | ||
287 | /* Python function to set the ignore count of a breakpoint. */ | |
288 | static int | |
289 | bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure) | |
290 | { | |
291 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
292 | long value; | |
293 | ||
294 | BPPY_SET_REQUIRE_VALID (self_bp); | |
295 | ||
296 | if (newvalue == NULL) | |
297 | { | |
298 | PyErr_SetString (PyExc_TypeError, | |
299 | _("Cannot delete `ignore_count' attribute.")); | |
300 | return -1; | |
301 | } | |
302 | else if (! PyInt_Check (newvalue)) | |
303 | { | |
304 | PyErr_SetString (PyExc_TypeError, | |
305 | _("The value of `ignore_count' must be an integer.")); | |
306 | return -1; | |
307 | } | |
308 | ||
309 | value = PyInt_AsLong (newvalue); | |
310 | if (value < 0) | |
311 | value = 0; | |
312 | set_ignore_count (self_bp->number, (int) value, 0); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | /* Python function to set the hit count of a breakpoint. */ | |
318 | static int | |
319 | bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure) | |
320 | { | |
321 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
322 | ||
323 | BPPY_SET_REQUIRE_VALID (self_bp); | |
324 | ||
325 | if (newvalue == NULL) | |
326 | { | |
327 | PyErr_SetString (PyExc_TypeError, | |
328 | _("Cannot delete `hit_count' attribute.")); | |
329 | return -1; | |
330 | } | |
331 | else if (! PyInt_Check (newvalue) || PyInt_AsLong (newvalue) != 0) | |
332 | { | |
333 | PyErr_SetString (PyExc_AttributeError, | |
334 | _("The value of `hit_count' must be zero.")); | |
335 | return -1; | |
336 | } | |
337 | ||
338 | self_bp->bp->hit_count = 0; | |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
343 | /* Python function to get the location of a breakpoint. */ | |
344 | static PyObject * | |
345 | bppy_get_location (PyObject *self, void *closure) | |
346 | { | |
347 | char *str; | |
348 | breakpoint_object *obj = (breakpoint_object *) self; | |
349 | ||
350 | BPPY_REQUIRE_VALID (obj); | |
351 | ||
352 | if (obj->bp->type != bp_breakpoint) | |
353 | Py_RETURN_NONE; | |
354 | ||
355 | str = obj->bp->addr_string; | |
356 | ||
357 | if (! str) | |
358 | str = ""; | |
359 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
360 | } | |
361 | ||
362 | /* Python function to get the breakpoint expression. */ | |
363 | static PyObject * | |
364 | bppy_get_expression (PyObject *self, void *closure) | |
365 | { | |
366 | char *str; | |
367 | breakpoint_object *obj = (breakpoint_object *) self; | |
368 | ||
369 | BPPY_REQUIRE_VALID (obj); | |
370 | ||
371 | if (obj->bp->type != bp_watchpoint | |
372 | && obj->bp->type != bp_hardware_watchpoint | |
373 | && obj->bp->type != bp_read_watchpoint | |
374 | && obj->bp->type != bp_access_watchpoint) | |
375 | Py_RETURN_NONE; | |
376 | ||
377 | str = obj->bp->exp_string; | |
378 | if (! str) | |
379 | str = ""; | |
380 | ||
381 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
382 | } | |
383 | ||
384 | /* Python function to get the condition expression of a breakpoint. */ | |
385 | static PyObject * | |
386 | bppy_get_condition (PyObject *self, void *closure) | |
387 | { | |
388 | char *str; | |
389 | breakpoint_object *obj = (breakpoint_object *) self; | |
390 | ||
391 | BPPY_REQUIRE_VALID (obj); | |
392 | ||
393 | str = obj->bp->cond_string; | |
394 | if (! str) | |
395 | Py_RETURN_NONE; | |
396 | ||
397 | return PyString_Decode (str, strlen (str), host_charset (), NULL); | |
398 | } | |
399 | ||
8dc78533 JK |
400 | /* Returns 0 on success. Returns -1 on error, with a python exception set. |
401 | */ | |
402 | ||
adc36818 PM |
403 | static int |
404 | bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure) | |
405 | { | |
406 | char *exp; | |
407 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
408 | volatile struct gdb_exception except; | |
409 | ||
410 | BPPY_SET_REQUIRE_VALID (self_bp); | |
411 | ||
412 | if (newvalue == NULL) | |
413 | { | |
414 | PyErr_SetString (PyExc_TypeError, | |
415 | _("Cannot delete `condition' attribute.")); | |
416 | return -1; | |
417 | } | |
418 | else if (newvalue == Py_None) | |
419 | exp = ""; | |
420 | else | |
421 | { | |
422 | exp = python_string_to_host_string (newvalue); | |
423 | if (exp == NULL) | |
424 | return -1; | |
425 | } | |
426 | ||
427 | TRY_CATCH (except, RETURN_MASK_ALL) | |
428 | { | |
429 | set_breakpoint_condition (self_bp->bp, exp, 0); | |
430 | } | |
431 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
436 | /* Python function to get the commands attached to a breakpoint. */ | |
437 | static PyObject * | |
438 | bppy_get_commands (PyObject *self, void *closure) | |
439 | { | |
440 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
441 | struct breakpoint *bp = self_bp->bp; | |
442 | long length; | |
443 | volatile struct gdb_exception except; | |
444 | struct ui_file *string_file; | |
445 | struct cleanup *chain; | |
446 | PyObject *result; | |
447 | char *cmdstr; | |
448 | ||
449 | BPPY_REQUIRE_VALID (self_bp); | |
450 | ||
451 | if (! self_bp->bp->commands) | |
452 | Py_RETURN_NONE; | |
453 | ||
454 | string_file = mem_fileopen (); | |
455 | chain = make_cleanup_ui_file_delete (string_file); | |
456 | ||
14dba4b4 | 457 | ui_out_redirect (uiout, string_file); |
adc36818 PM |
458 | TRY_CATCH (except, RETURN_MASK_ALL) |
459 | { | |
adc36818 | 460 | print_command_lines (uiout, breakpoint_commands (bp), 0); |
adc36818 | 461 | } |
14dba4b4 | 462 | ui_out_redirect (uiout, NULL); |
adc36818 PM |
463 | cmdstr = ui_file_xstrdup (string_file, &length); |
464 | GDB_PY_HANDLE_EXCEPTION (except); | |
465 | ||
466 | result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL); | |
467 | do_cleanups (chain); | |
468 | xfree (cmdstr); | |
469 | return result; | |
470 | } | |
471 | ||
472 | /* Python function to get the breakpoint type. */ | |
473 | static PyObject * | |
474 | bppy_get_type (PyObject *self, void *closure) | |
475 | { | |
476 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
477 | ||
478 | BPPY_REQUIRE_VALID (self_bp); | |
479 | ||
480 | return PyInt_FromLong (self_bp->bp->type); | |
481 | } | |
482 | ||
84f4c1fe PM |
483 | /* Python function to get the visibility of the breakpoint. */ |
484 | ||
485 | static PyObject * | |
486 | bppy_get_visibility (PyObject *self, void *closure) | |
487 | { | |
488 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
489 | ||
490 | BPPY_REQUIRE_VALID (self_bp); | |
491 | ||
492 | if (self_bp->bp->number < 0) | |
493 | Py_RETURN_FALSE; | |
494 | ||
495 | Py_RETURN_TRUE; | |
496 | } | |
497 | ||
adc36818 PM |
498 | /* Python function to get the breakpoint's number. */ |
499 | static PyObject * | |
500 | bppy_get_number (PyObject *self, void *closure) | |
501 | { | |
502 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
503 | ||
504 | BPPY_REQUIRE_VALID (self_bp); | |
505 | ||
506 | return PyInt_FromLong (self_bp->number); | |
507 | } | |
508 | ||
509 | /* Python function to get the breakpoint's thread ID. */ | |
510 | static PyObject * | |
511 | bppy_get_thread (PyObject *self, void *closure) | |
512 | { | |
513 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
514 | ||
515 | BPPY_REQUIRE_VALID (self_bp); | |
516 | ||
517 | if (self_bp->bp->thread == -1) | |
518 | Py_RETURN_NONE; | |
519 | ||
520 | return PyInt_FromLong (self_bp->bp->thread); | |
521 | } | |
522 | ||
523 | /* Python function to get the breakpoint's task ID (in Ada). */ | |
524 | static PyObject * | |
525 | bppy_get_task (PyObject *self, void *closure) | |
526 | { | |
527 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
528 | ||
529 | BPPY_REQUIRE_VALID (self_bp); | |
530 | ||
531 | if (self_bp->bp->task == 0) | |
532 | Py_RETURN_NONE; | |
533 | ||
534 | return PyInt_FromLong (self_bp->bp->task); | |
535 | } | |
536 | ||
537 | /* Python function to get the breakpoint's hit count. */ | |
538 | static PyObject * | |
539 | bppy_get_hit_count (PyObject *self, void *closure) | |
540 | { | |
541 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
542 | ||
543 | BPPY_REQUIRE_VALID (self_bp); | |
544 | ||
545 | return PyInt_FromLong (self_bp->bp->hit_count); | |
546 | } | |
547 | ||
548 | /* Python function to get the breakpoint's ignore count. */ | |
549 | static PyObject * | |
550 | bppy_get_ignore_count (PyObject *self, void *closure) | |
551 | { | |
552 | breakpoint_object *self_bp = (breakpoint_object *) self; | |
553 | ||
554 | BPPY_REQUIRE_VALID (self_bp); | |
555 | ||
556 | return PyInt_FromLong (self_bp->bp->ignore_count); | |
557 | } | |
558 | ||
559 | /* Python function to create a new breakpoint. */ | |
560 | static PyObject * | |
561 | bppy_new (PyTypeObject *subtype, PyObject *args, PyObject *kwargs) | |
562 | { | |
563 | PyObject *result; | |
84f4c1fe | 564 | static char *keywords[] = { "spec", "type", "wp_class", "internal", NULL }; |
adc36818 PM |
565 | char *spec; |
566 | int type = bp_breakpoint; | |
567 | int access_type = hw_write; | |
84f4c1fe PM |
568 | PyObject *internal = NULL; |
569 | int internal_bp = 0; | |
adc36818 PM |
570 | volatile struct gdb_exception except; |
571 | ||
84f4c1fe PM |
572 | if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiO", keywords, |
573 | &spec, &type, &access_type, &internal)) | |
adc36818 PM |
574 | return NULL; |
575 | ||
84f4c1fe PM |
576 | if (internal) |
577 | { | |
578 | internal_bp = PyObject_IsTrue (internal); | |
579 | if (internal_bp == -1) | |
580 | return NULL; | |
581 | } | |
582 | ||
adc36818 PM |
583 | result = subtype->tp_alloc (subtype, 0); |
584 | if (! result) | |
585 | return NULL; | |
586 | bppy_pending_object = (breakpoint_object *) result; | |
587 | bppy_pending_object->number = -1; | |
588 | bppy_pending_object->bp = NULL; | |
589 | ||
590 | TRY_CATCH (except, RETURN_MASK_ALL) | |
591 | { | |
592 | switch (type) | |
593 | { | |
594 | case bp_breakpoint: | |
595 | { | |
596 | create_breakpoint (python_gdbarch, | |
597 | spec, NULL, -1, | |
598 | 0, | |
0fb4aa4b | 599 | 0, bp_breakpoint, |
adc36818 PM |
600 | 0, |
601 | AUTO_BOOLEAN_TRUE, | |
84f4c1fe | 602 | NULL, 0, 1, internal_bp); |
adc36818 PM |
603 | break; |
604 | } | |
605 | case bp_watchpoint: | |
606 | { | |
607 | if (access_type == hw_write) | |
84f4c1fe | 608 | watch_command_wrapper (spec, 0, internal_bp); |
adc36818 | 609 | else if (access_type == hw_access) |
84f4c1fe | 610 | awatch_command_wrapper (spec, 0, internal_bp); |
adc36818 | 611 | else if (access_type == hw_read) |
84f4c1fe | 612 | rwatch_command_wrapper (spec, 0, internal_bp); |
adc36818 PM |
613 | else |
614 | error(_("Cannot understand watchpoint access type.")); | |
615 | break; | |
616 | } | |
617 | default: | |
618 | error(_("Do not understand breakpoint type to set.")); | |
619 | } | |
620 | } | |
621 | if (except.reason < 0) | |
622 | { | |
623 | subtype->tp_free (result); | |
624 | return PyErr_Format (except.reason == RETURN_QUIT | |
625 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
626 | "%s", except.message); | |
627 | } | |
628 | ||
629 | BPPY_REQUIRE_VALID ((breakpoint_object *) result); | |
630 | return result; | |
631 | } | |
632 | ||
633 | \f | |
634 | ||
84f4c1fe PM |
635 | static int |
636 | build_bp_list (struct breakpoint *b, void *arg) | |
637 | { | |
638 | PyObject *list = arg; | |
639 | PyObject *bp = b->py_bp_object; | |
640 | int iserr = 0; | |
641 | ||
642 | /* Not all breakpoints will have a companion Python object. | |
643 | Only breakpoints that were created via bppy_new, or | |
644 | breakpoints that were created externally and are tracked by | |
645 | the Python Scripting API. */ | |
646 | if (bp) | |
647 | iserr = PyList_Append (list, bp); | |
648 | ||
649 | if (iserr == -1) | |
650 | return 1; | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
adc36818 PM |
655 | /* Static function to return a tuple holding all breakpoints. */ |
656 | ||
657 | PyObject * | |
658 | gdbpy_breakpoints (PyObject *self, PyObject *args) | |
659 | { | |
84f4c1fe | 660 | PyObject *list; |
adc36818 PM |
661 | |
662 | if (bppy_live == 0) | |
663 | Py_RETURN_NONE; | |
664 | ||
84f4c1fe PM |
665 | list = PyList_New (0); |
666 | if (!list) | |
667 | return NULL; | |
d59b6f6c | 668 | |
84f4c1fe PM |
669 | /* If iteratre_over_breakpoints returns non NULL it signals an error |
670 | condition. In that case abandon building the list and return | |
671 | NULL. */ | |
672 | if (iterate_over_breakpoints (build_bp_list, list) != NULL) | |
673 | { | |
674 | Py_DECREF (list); | |
675 | return NULL; | |
adc36818 | 676 | } |
84f4c1fe PM |
677 | |
678 | return PyList_AsTuple (list); | |
adc36818 PM |
679 | } |
680 | ||
681 | \f | |
682 | ||
683 | /* Event callback functions. */ | |
684 | ||
685 | /* Callback that is used when a breakpoint is created. This function | |
686 | will create a new Python breakpoint object. */ | |
687 | static void | |
688 | gdbpy_breakpoint_created (int num) | |
689 | { | |
690 | breakpoint_object *newbp; | |
691 | struct breakpoint *bp = NULL; | |
692 | PyGILState_STATE state; | |
693 | ||
adc36818 PM |
694 | bp = get_breakpoint (num); |
695 | if (! bp) | |
696 | return; | |
697 | ||
84f4c1fe PM |
698 | if (num < 0 && bppy_pending_object == NULL) |
699 | return; | |
700 | ||
adc36818 PM |
701 | if (bp->type != bp_breakpoint |
702 | && bp->type != bp_watchpoint | |
703 | && bp->type != bp_hardware_watchpoint | |
704 | && bp->type != bp_read_watchpoint | |
705 | && bp->type != bp_access_watchpoint) | |
706 | return; | |
707 | ||
adc36818 PM |
708 | state = PyGILState_Ensure (); |
709 | ||
710 | if (bppy_pending_object) | |
711 | { | |
712 | newbp = bppy_pending_object; | |
713 | bppy_pending_object = NULL; | |
714 | } | |
715 | else | |
716 | newbp = PyObject_New (breakpoint_object, &breakpoint_object_type); | |
717 | if (newbp) | |
718 | { | |
adc36818 PM |
719 | newbp->number = num; |
720 | newbp->bp = bp; | |
84f4c1fe | 721 | newbp->bp->py_bp_object = (PyObject *) newbp; |
adc36818 | 722 | Py_INCREF (newbp); |
84f4c1fe PM |
723 | ++bppy_live; |
724 | } | |
725 | else | |
726 | { | |
727 | PyErr_SetString (PyExc_RuntimeError, | |
728 | _("Error while creating breakpoint from GDB.")); | |
729 | gdbpy_print_stack (); | |
adc36818 | 730 | } |
adc36818 PM |
731 | |
732 | PyGILState_Release (state); | |
733 | } | |
734 | ||
735 | /* Callback that is used when a breakpoint is deleted. This will | |
736 | invalidate the corresponding Python object. */ | |
737 | static void | |
738 | gdbpy_breakpoint_deleted (int num) | |
739 | { | |
740 | PyGILState_STATE state; | |
84f4c1fe PM |
741 | struct breakpoint *bp = NULL; |
742 | breakpoint_object *bp_obj; | |
adc36818 PM |
743 | |
744 | state = PyGILState_Ensure (); | |
84f4c1fe PM |
745 | bp = get_breakpoint (num); |
746 | if (! bp) | |
747 | return; | |
748 | ||
749 | bp_obj = ((breakpoint_object *) bp->py_bp_object); | |
750 | if (bp_obj) | |
adc36818 | 751 | { |
84f4c1fe | 752 | bp_obj->bp = NULL; |
adc36818 | 753 | --bppy_live; |
84f4c1fe | 754 | Py_DECREF (bp_obj); |
adc36818 PM |
755 | } |
756 | PyGILState_Release (state); | |
757 | } | |
758 | ||
759 | \f | |
760 | ||
761 | /* Initialize the Python breakpoint code. */ | |
762 | void | |
763 | gdbpy_initialize_breakpoints (void) | |
764 | { | |
765 | int i; | |
766 | ||
767 | breakpoint_object_type.tp_new = bppy_new; | |
768 | if (PyType_Ready (&breakpoint_object_type) < 0) | |
769 | return; | |
770 | ||
771 | Py_INCREF (&breakpoint_object_type); | |
772 | PyModule_AddObject (gdb_module, "Breakpoint", | |
773 | (PyObject *) &breakpoint_object_type); | |
774 | ||
775 | observer_attach_breakpoint_created (gdbpy_breakpoint_created); | |
776 | observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted); | |
777 | ||
778 | /* Add breakpoint types constants. */ | |
779 | for (i = 0; pybp_codes[i].name; ++i) | |
780 | { | |
781 | if (PyModule_AddIntConstant (gdb_module, | |
782 | /* Cast needed for Python 2.4. */ | |
783 | (char *) pybp_codes[i].name, | |
784 | pybp_codes[i].code) < 0) | |
785 | return; | |
786 | } | |
787 | ||
788 | /* Add watchpoint types constants. */ | |
789 | for (i = 0; pybp_watch_types[i].name; ++i) | |
790 | { | |
791 | if (PyModule_AddIntConstant (gdb_module, | |
792 | /* Cast needed for Python 2.4. */ | |
793 | (char *) pybp_watch_types[i].name, | |
794 | pybp_watch_types[i].code) < 0) | |
795 | return; | |
796 | } | |
797 | ||
798 | } | |
799 | ||
800 | \f | |
801 | ||
802 | static PyGetSetDef breakpoint_object_getset[] = { | |
803 | { "enabled", bppy_get_enabled, bppy_set_enabled, | |
804 | "Boolean telling whether the breakpoint is enabled.", NULL }, | |
805 | { "silent", bppy_get_silent, bppy_set_silent, | |
806 | "Boolean telling whether the breakpoint is silent.", NULL }, | |
807 | { "thread", bppy_get_thread, bppy_set_thread, | |
808 | "Thread ID for the breakpoint.\n\ | |
809 | If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\ | |
810 | If the value is None, then this breakpoint is not thread-specific.\n\ | |
811 | No other type of value can be used.", NULL }, | |
812 | { "task", bppy_get_task, bppy_set_task, | |
813 | "Thread ID for the breakpoint.\n\ | |
814 | If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\ | |
815 | If the value is None, then this breakpoint is not task-specific.\n\ | |
816 | No other type of value can be used.", NULL }, | |
817 | { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count, | |
818 | "Number of times this breakpoint should be automatically continued.", | |
819 | NULL }, | |
820 | { "number", bppy_get_number, NULL, | |
821 | "Breakpoint's number assigned by GDB.", NULL }, | |
822 | { "hit_count", bppy_get_hit_count, bppy_set_hit_count, | |
823 | "Number of times the breakpoint has been hit.\n\ | |
824 | Can be set to zero to clear the count. No other value is valid\n\ | |
825 | when setting this property.", NULL }, | |
826 | { "location", bppy_get_location, NULL, | |
827 | "Location of the breakpoint, as specified by the user.", NULL}, | |
828 | { "expression", bppy_get_expression, NULL, | |
829 | "Expression of the breakpoint, as specified by the user.", NULL}, | |
830 | { "condition", bppy_get_condition, bppy_set_condition, | |
831 | "Condition of the breakpoint, as specified by the user,\ | |
832 | or None if no condition set."}, | |
833 | { "commands", bppy_get_commands, NULL, | |
834 | "Commands of the breakpoint, as specified by the user."}, | |
835 | { "type", bppy_get_type, NULL, | |
836 | "Type of breakpoint."}, | |
84f4c1fe PM |
837 | { "visible", bppy_get_visibility, NULL, |
838 | "Whether the breakpoint is visible to the user."}, | |
adc36818 PM |
839 | { NULL } /* Sentinel. */ |
840 | }; | |
841 | ||
842 | static PyMethodDef breakpoint_object_methods[] = | |
843 | { | |
844 | { "is_valid", bppy_is_valid, METH_NOARGS, | |
845 | "Return true if this breakpoint is valid, false if not." }, | |
846 | { NULL } /* Sentinel. */ | |
847 | }; | |
848 | ||
849 | static PyTypeObject breakpoint_object_type = | |
850 | { | |
851 | PyObject_HEAD_INIT (NULL) | |
852 | 0, /*ob_size*/ | |
853 | "gdb.Breakpoint", /*tp_name*/ | |
854 | sizeof (breakpoint_object), /*tp_basicsize*/ | |
855 | 0, /*tp_itemsize*/ | |
856 | 0, /*tp_dealloc*/ | |
857 | 0, /*tp_print*/ | |
858 | 0, /*tp_getattr*/ | |
859 | 0, /*tp_setattr*/ | |
860 | 0, /*tp_compare*/ | |
861 | 0, /*tp_repr*/ | |
862 | 0, /*tp_as_number*/ | |
863 | 0, /*tp_as_sequence*/ | |
864 | 0, /*tp_as_mapping*/ | |
865 | 0, /*tp_hash */ | |
866 | 0, /*tp_call*/ | |
867 | 0, /*tp_str*/ | |
868 | 0, /*tp_getattro*/ | |
869 | 0, /*tp_setattro*/ | |
870 | 0, /*tp_as_buffer*/ | |
871 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
872 | "GDB breakpoint object", /* tp_doc */ | |
873 | 0, /* tp_traverse */ | |
874 | 0, /* tp_clear */ | |
875 | 0, /* tp_richcompare */ | |
876 | 0, /* tp_weaklistoffset */ | |
877 | 0, /* tp_iter */ | |
878 | 0, /* tp_iternext */ | |
879 | breakpoint_object_methods, /* tp_methods */ | |
880 | 0, /* tp_members */ | |
881 | breakpoint_object_getset /* tp_getset */ | |
882 | }; |