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