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