1 1.1 christos /* Python interface to breakpoints 2 1.1 christos 3 1.11 christos Copyright (C) 2008-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "value.h" 21 1.1 christos #include "python-internal.h" 22 1.1 christos #include "python.h" 23 1.1 christos #include "charset.h" 24 1.1 christos #include "breakpoint.h" 25 1.11 christos #include "cli/cli-cmds.h" 26 1.1 christos #include "gdbthread.h" 27 1.8 christos #include "observable.h" 28 1.1 christos #include "cli/cli-script.h" 29 1.1 christos #include "ada-lang.h" 30 1.1 christos #include "arch-utils.h" 31 1.1 christos #include "language.h" 32 1.6 christos #include "location.h" 33 1.6 christos #include "py-event.h" 34 1.8 christos #include "linespec.h" 35 1.11 christos #include "gdbsupport/common-utils.h" 36 1.1 christos 37 1.10 christos extern PyTypeObject breakpoint_location_object_type 38 1.10 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("breakpoint_location_object"); 39 1.10 christos 40 1.10 christos struct gdbpy_breakpoint_location_object 41 1.10 christos { 42 1.10 christos PyObject_HEAD 43 1.10 christos 44 1.10 christos /* An owning reference to the gdb breakpoint location object. */ 45 1.10 christos bp_location *bp_loc; 46 1.10 christos 47 1.10 christos /* An owning reference to the location's breakpoint owner. */ 48 1.10 christos gdbpy_breakpoint_object *owner; 49 1.10 christos }; 50 1.10 christos 51 1.10 christos /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python 52 1.10 christos exception if they are not. */ 53 1.10 christos #define BPLOCPY_REQUIRE_VALID(Breakpoint, Location) \ 54 1.10 christos do { \ 55 1.10 christos if ((Breakpoint)->bp != (Location)->bp_loc->owner) \ 56 1.10 christos return PyErr_Format (PyExc_RuntimeError, \ 57 1.10 christos _("Breakpoint location is invalid.")); \ 58 1.10 christos } while (0) 59 1.10 christos 60 1.10 christos /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python 61 1.10 christos exception if they are not. This macro is for use in setter functions. */ 62 1.10 christos #define BPLOCPY_SET_REQUIRE_VALID(Breakpoint, Location) \ 63 1.10 christos do { \ 64 1.10 christos if ((Breakpoint)->bp != (Location)->bp_loc->owner) \ 65 1.10 christos { \ 66 1.10 christos PyErr_Format (PyExc_RuntimeError, \ 67 1.10 christos _("Breakpoint location is invalid.")); \ 68 1.10 christos return -1; \ 69 1.10 christos } \ 70 1.10 christos } while (0) 71 1.10 christos 72 1.10 christos /* Debugging of Python breakpoints. */ 73 1.10 christos 74 1.10 christos static bool pybp_debug; 75 1.10 christos 76 1.10 christos /* Implementation of "show debug py-breakpoint". */ 77 1.10 christos 78 1.10 christos static void 79 1.10 christos show_pybp_debug (struct ui_file *file, int from_tty, 80 1.10 christos struct cmd_list_element *c, const char *value) 81 1.10 christos { 82 1.10 christos gdb_printf (file, _("Python breakpoint debugging is %s.\n"), value); 83 1.10 christos } 84 1.10 christos 85 1.10 christos /* Print a "py-breakpoint" debug statement. */ 86 1.10 christos 87 1.10 christos #define pybp_debug_printf(fmt, ...) \ 88 1.10 christos debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__) 89 1.10 christos 90 1.10 christos /* Print a "py-breakpoint" enter/exit debug statements. */ 91 1.10 christos 92 1.10 christos #define PYBP_SCOPED_DEBUG_ENTER_EXIT \ 93 1.10 christos scoped_debug_enter_exit (pybp_debug, "py-breakpoint") 94 1.10 christos 95 1.1 christos /* Number of live breakpoints. */ 96 1.1 christos static int bppy_live; 97 1.1 christos 98 1.1 christos /* Variables used to pass information between the Breakpoint 99 1.1 christos constructor and the breakpoint-created hook function. */ 100 1.1 christos gdbpy_breakpoint_object *bppy_pending_object; 101 1.1 christos 102 1.1 christos /* Function that is called when a Python condition is evaluated. */ 103 1.7 christos static const char stop_func[] = "stop"; 104 1.1 christos 105 1.1 christos /* This is used to initialize various gdb.bp_* constants. */ 106 1.1 christos struct pybp_code 107 1.1 christos { 108 1.1 christos /* The name. */ 109 1.1 christos const char *name; 110 1.1 christos /* The code. */ 111 1.1 christos int code; 112 1.1 christos }; 113 1.1 christos 114 1.1 christos /* Entries related to the type of user set breakpoints. */ 115 1.1 christos static struct pybp_code pybp_codes[] = 116 1.1 christos { 117 1.1 christos { "BP_NONE", bp_none}, 118 1.1 christos { "BP_BREAKPOINT", bp_breakpoint}, 119 1.10 christos { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint}, 120 1.1 christos { "BP_WATCHPOINT", bp_watchpoint}, 121 1.1 christos { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint}, 122 1.1 christos { "BP_READ_WATCHPOINT", bp_read_watchpoint}, 123 1.1 christos { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint}, 124 1.10 christos { "BP_CATCHPOINT", bp_catchpoint}, 125 1.1 christos {NULL} /* Sentinel. */ 126 1.1 christos }; 127 1.1 christos 128 1.1 christos /* Entries related to the type of watchpoint. */ 129 1.1 christos static struct pybp_code pybp_watch_types[] = 130 1.1 christos { 131 1.1 christos { "WP_READ", hw_read}, 132 1.1 christos { "WP_WRITE", hw_write}, 133 1.1 christos { "WP_ACCESS", hw_access}, 134 1.1 christos {NULL} /* Sentinel. */ 135 1.1 christos }; 136 1.1 christos 137 1.1 christos /* Python function which checks the validity of a breakpoint object. */ 138 1.1 christos static PyObject * 139 1.1 christos bppy_is_valid (PyObject *self, PyObject *args) 140 1.1 christos { 141 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 142 1.1 christos 143 1.1 christos if (self_bp->bp) 144 1.1 christos Py_RETURN_TRUE; 145 1.1 christos Py_RETURN_FALSE; 146 1.1 christos } 147 1.1 christos 148 1.1 christos /* Python function to test whether or not the breakpoint is enabled. */ 149 1.1 christos static PyObject * 150 1.1 christos bppy_get_enabled (PyObject *self, void *closure) 151 1.1 christos { 152 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 153 1.1 christos 154 1.1 christos BPPY_REQUIRE_VALID (self_bp); 155 1.1 christos if (! self_bp->bp) 156 1.1 christos Py_RETURN_FALSE; 157 1.1 christos if (self_bp->bp->enable_state == bp_enabled) 158 1.1 christos Py_RETURN_TRUE; 159 1.1 christos Py_RETURN_FALSE; 160 1.1 christos } 161 1.1 christos 162 1.1 christos /* Python function to test whether or not the breakpoint is silent. */ 163 1.1 christos static PyObject * 164 1.1 christos bppy_get_silent (PyObject *self, void *closure) 165 1.1 christos { 166 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 167 1.1 christos 168 1.1 christos BPPY_REQUIRE_VALID (self_bp); 169 1.1 christos if (self_bp->bp->silent) 170 1.1 christos Py_RETURN_TRUE; 171 1.1 christos Py_RETURN_FALSE; 172 1.1 christos } 173 1.1 christos 174 1.1 christos /* Python function to set the enabled state of a breakpoint. */ 175 1.1 christos static int 176 1.1 christos bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure) 177 1.1 christos { 178 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 179 1.1 christos int cmp; 180 1.1 christos 181 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 182 1.1 christos 183 1.1 christos if (newvalue == NULL) 184 1.1 christos { 185 1.1 christos PyErr_SetString (PyExc_TypeError, 186 1.1 christos _("Cannot delete `enabled' attribute.")); 187 1.1 christos 188 1.1 christos return -1; 189 1.1 christos } 190 1.1 christos else if (! PyBool_Check (newvalue)) 191 1.1 christos { 192 1.1 christos PyErr_SetString (PyExc_TypeError, 193 1.1 christos _("The value of `enabled' must be a boolean.")); 194 1.1 christos return -1; 195 1.1 christos } 196 1.1 christos 197 1.1 christos cmp = PyObject_IsTrue (newvalue); 198 1.1 christos if (cmp < 0) 199 1.1 christos return -1; 200 1.1 christos 201 1.9 christos try 202 1.1 christos { 203 1.1 christos if (cmp == 1) 204 1.1 christos enable_breakpoint (self_bp->bp); 205 1.1 christos else 206 1.1 christos disable_breakpoint (self_bp->bp); 207 1.1 christos } 208 1.9 christos catch (const gdb_exception &except) 209 1.5 christos { 210 1.12 christos return gdbpy_handle_gdb_exception (-1, except); 211 1.5 christos } 212 1.1 christos 213 1.1 christos return 0; 214 1.1 christos } 215 1.1 christos 216 1.1 christos /* Python function to set the 'silent' state of a breakpoint. */ 217 1.1 christos static int 218 1.1 christos bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure) 219 1.1 christos { 220 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 221 1.1 christos int cmp; 222 1.1 christos 223 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 224 1.1 christos 225 1.1 christos if (newvalue == NULL) 226 1.1 christos { 227 1.1 christos PyErr_SetString (PyExc_TypeError, 228 1.1 christos _("Cannot delete `silent' attribute.")); 229 1.1 christos return -1; 230 1.1 christos } 231 1.1 christos else if (! PyBool_Check (newvalue)) 232 1.1 christos { 233 1.1 christos PyErr_SetString (PyExc_TypeError, 234 1.1 christos _("The value of `silent' must be a boolean.")); 235 1.1 christos return -1; 236 1.1 christos } 237 1.1 christos 238 1.1 christos cmp = PyObject_IsTrue (newvalue); 239 1.1 christos if (cmp < 0) 240 1.1 christos return -1; 241 1.1 christos else 242 1.1 christos breakpoint_set_silent (self_bp->bp, cmp); 243 1.1 christos 244 1.1 christos return 0; 245 1.1 christos } 246 1.1 christos 247 1.1 christos /* Python function to set the thread of a breakpoint. */ 248 1.1 christos static int 249 1.1 christos bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure) 250 1.1 christos { 251 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 252 1.1 christos long id; 253 1.1 christos 254 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 255 1.1 christos 256 1.1 christos if (newvalue == NULL) 257 1.1 christos { 258 1.1 christos PyErr_SetString (PyExc_TypeError, 259 1.1 christos _("Cannot delete `thread' attribute.")); 260 1.1 christos return -1; 261 1.1 christos } 262 1.10 christos else if (PyLong_Check (newvalue)) 263 1.1 christos { 264 1.1 christos if (! gdb_py_int_as_long (newvalue, &id)) 265 1.1 christos return -1; 266 1.1 christos 267 1.6 christos if (!valid_global_thread_id (id)) 268 1.1 christos { 269 1.1 christos PyErr_SetString (PyExc_RuntimeError, 270 1.1 christos _("Invalid thread ID.")); 271 1.1 christos return -1; 272 1.1 christos } 273 1.11 christos 274 1.11 christos if (self_bp->bp->task != -1) 275 1.11 christos { 276 1.11 christos PyErr_SetString (PyExc_RuntimeError, 277 1.11 christos _("Cannot set both task and thread attributes.")); 278 1.11 christos return -1; 279 1.11 christos } 280 1.1 christos } 281 1.1 christos else if (newvalue == Py_None) 282 1.1 christos id = -1; 283 1.1 christos else 284 1.1 christos { 285 1.1 christos PyErr_SetString (PyExc_TypeError, 286 1.1 christos _("The value of `thread' must be an integer or None.")); 287 1.1 christos return -1; 288 1.1 christos } 289 1.1 christos 290 1.11 christos if (self_bp->bp->inferior != -1 && id != -1) 291 1.11 christos { 292 1.11 christos PyErr_SetString (PyExc_RuntimeError, 293 1.11 christos _("Cannot have both 'thread' and 'inferior' " 294 1.11 christos "conditions on a breakpoint")); 295 1.11 christos return -1; 296 1.11 christos } 297 1.11 christos 298 1.1 christos breakpoint_set_thread (self_bp->bp, id); 299 1.1 christos 300 1.1 christos return 0; 301 1.1 christos } 302 1.1 christos 303 1.11 christos /* Python function to set the inferior of a breakpoint. */ 304 1.11 christos 305 1.11 christos static int 306 1.11 christos bppy_set_inferior (PyObject *self, PyObject *newvalue, void *closure) 307 1.11 christos { 308 1.11 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 309 1.11 christos long id; 310 1.11 christos 311 1.11 christos BPPY_SET_REQUIRE_VALID (self_bp); 312 1.11 christos 313 1.11 christos if (newvalue == NULL) 314 1.11 christos { 315 1.11 christos PyErr_SetString (PyExc_TypeError, 316 1.11 christos _("Cannot delete 'inferior' attribute.")); 317 1.11 christos return -1; 318 1.11 christos } 319 1.11 christos else if (PyLong_Check (newvalue)) 320 1.11 christos { 321 1.11 christos if (!gdb_py_int_as_long (newvalue, &id)) 322 1.11 christos return -1; 323 1.11 christos 324 1.11 christos if (!valid_global_inferior_id (id)) 325 1.11 christos { 326 1.11 christos PyErr_SetString (PyExc_RuntimeError, 327 1.11 christos _("Invalid inferior ID.")); 328 1.11 christos return -1; 329 1.11 christos } 330 1.11 christos } 331 1.11 christos else if (newvalue == Py_None) 332 1.11 christos id = -1; 333 1.11 christos else 334 1.11 christos { 335 1.11 christos PyErr_SetString (PyExc_TypeError, 336 1.11 christos _("The value of 'inferior' must be an integer or None.")); 337 1.11 christos return -1; 338 1.11 christos } 339 1.11 christos 340 1.11 christos if (self_bp->bp->type != bp_breakpoint 341 1.11 christos && self_bp->bp->type != bp_hardware_breakpoint) 342 1.11 christos { 343 1.11 christos PyErr_SetString (PyExc_RuntimeError, 344 1.11 christos _("Cannot set 'inferior' attribute on a gdb.Breakpoint " 345 1.11 christos "of this type")); 346 1.11 christos return -1; 347 1.11 christos } 348 1.11 christos 349 1.11 christos if (self_bp->bp->thread != -1 && id != -1) 350 1.11 christos { 351 1.11 christos PyErr_SetString (PyExc_RuntimeError, 352 1.11 christos _("Cannot have both 'thread' and 'inferior' conditions " 353 1.11 christos "on a breakpoint")); 354 1.11 christos return -1; 355 1.11 christos } 356 1.11 christos 357 1.11 christos if (self_bp->bp->task != -1 && id != -1) 358 1.11 christos { 359 1.11 christos PyErr_SetString (PyExc_RuntimeError, 360 1.11 christos _("Cannot have both 'task' and 'inferior' conditions " 361 1.11 christos "on a breakpoint")); 362 1.11 christos return -1; 363 1.11 christos } 364 1.11 christos 365 1.11 christos breakpoint_set_inferior (self_bp->bp, id); 366 1.11 christos 367 1.11 christos return 0; 368 1.11 christos } 369 1.11 christos 370 1.1 christos /* Python function to set the (Ada) task of a breakpoint. */ 371 1.1 christos static int 372 1.1 christos bppy_set_task (PyObject *self, PyObject *newvalue, void *closure) 373 1.1 christos { 374 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 375 1.1 christos long id; 376 1.1 christos int valid_id = 0; 377 1.1 christos 378 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 379 1.1 christos 380 1.1 christos if (newvalue == NULL) 381 1.1 christos { 382 1.1 christos PyErr_SetString (PyExc_TypeError, 383 1.1 christos _("Cannot delete `task' attribute.")); 384 1.1 christos return -1; 385 1.1 christos } 386 1.10 christos else if (PyLong_Check (newvalue)) 387 1.1 christos { 388 1.1 christos if (! gdb_py_int_as_long (newvalue, &id)) 389 1.1 christos return -1; 390 1.1 christos 391 1.9 christos try 392 1.1 christos { 393 1.1 christos valid_id = valid_task_id (id); 394 1.1 christos } 395 1.9 christos catch (const gdb_exception &except) 396 1.5 christos { 397 1.12 christos return gdbpy_handle_gdb_exception (-1, except); 398 1.5 christos } 399 1.1 christos 400 1.1 christos if (! valid_id) 401 1.1 christos { 402 1.1 christos PyErr_SetString (PyExc_RuntimeError, 403 1.1 christos _("Invalid task ID.")); 404 1.1 christos return -1; 405 1.1 christos } 406 1.11 christos 407 1.11 christos if (self_bp->bp->thread != -1) 408 1.11 christos { 409 1.11 christos PyErr_SetString (PyExc_RuntimeError, 410 1.11 christos _("Cannot set both task and thread attributes.")); 411 1.11 christos return -1; 412 1.11 christos } 413 1.1 christos } 414 1.1 christos else if (newvalue == Py_None) 415 1.11 christos id = -1; 416 1.1 christos else 417 1.1 christos { 418 1.1 christos PyErr_SetString (PyExc_TypeError, 419 1.1 christos _("The value of `task' must be an integer or None.")); 420 1.1 christos return -1; 421 1.1 christos } 422 1.1 christos 423 1.1 christos breakpoint_set_task (self_bp->bp, id); 424 1.1 christos 425 1.1 christos return 0; 426 1.1 christos } 427 1.1 christos 428 1.1 christos /* Python function which deletes the underlying GDB breakpoint. This 429 1.1 christos triggers the breakpoint_deleted observer which will call 430 1.1 christos gdbpy_breakpoint_deleted; that function cleans up the Python 431 1.1 christos sections. */ 432 1.1 christos 433 1.1 christos static PyObject * 434 1.1 christos bppy_delete_breakpoint (PyObject *self, PyObject *args) 435 1.1 christos { 436 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 437 1.1 christos 438 1.1 christos BPPY_REQUIRE_VALID (self_bp); 439 1.1 christos 440 1.9 christos try 441 1.1 christos { 442 1.1 christos delete_breakpoint (self_bp->bp); 443 1.1 christos } 444 1.9 christos catch (const gdb_exception &except) 445 1.5 christos { 446 1.12 christos return gdbpy_handle_gdb_exception (nullptr, except); 447 1.5 christos } 448 1.1 christos 449 1.1 christos Py_RETURN_NONE; 450 1.1 christos } 451 1.1 christos 452 1.1 christos 453 1.1 christos /* Python function to set the ignore count of a breakpoint. */ 454 1.1 christos static int 455 1.1 christos bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure) 456 1.1 christos { 457 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 458 1.1 christos long value; 459 1.1 christos 460 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 461 1.1 christos 462 1.1 christos if (newvalue == NULL) 463 1.1 christos { 464 1.1 christos PyErr_SetString (PyExc_TypeError, 465 1.1 christos _("Cannot delete `ignore_count' attribute.")); 466 1.1 christos return -1; 467 1.1 christos } 468 1.10 christos else if (!PyLong_Check (newvalue)) 469 1.1 christos { 470 1.1 christos PyErr_SetString (PyExc_TypeError, 471 1.1 christos _("The value of `ignore_count' must be an integer.")); 472 1.1 christos return -1; 473 1.1 christos } 474 1.1 christos 475 1.1 christos if (! gdb_py_int_as_long (newvalue, &value)) 476 1.1 christos return -1; 477 1.1 christos 478 1.1 christos if (value < 0) 479 1.1 christos value = 0; 480 1.1 christos 481 1.9 christos try 482 1.1 christos { 483 1.1 christos set_ignore_count (self_bp->number, (int) value, 0); 484 1.1 christos } 485 1.9 christos catch (const gdb_exception &except) 486 1.5 christos { 487 1.12 christos return gdbpy_handle_gdb_exception (-1, except); 488 1.5 christos } 489 1.1 christos 490 1.1 christos return 0; 491 1.1 christos } 492 1.1 christos 493 1.1 christos /* Python function to set the hit count of a breakpoint. */ 494 1.1 christos static int 495 1.1 christos bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure) 496 1.1 christos { 497 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 498 1.1 christos 499 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 500 1.1 christos 501 1.1 christos if (newvalue == NULL) 502 1.1 christos { 503 1.1 christos PyErr_SetString (PyExc_TypeError, 504 1.1 christos _("Cannot delete `hit_count' attribute.")); 505 1.1 christos return -1; 506 1.1 christos } 507 1.1 christos else 508 1.1 christos { 509 1.1 christos long value; 510 1.1 christos 511 1.1 christos if (! gdb_py_int_as_long (newvalue, &value)) 512 1.1 christos return -1; 513 1.1 christos 514 1.1 christos if (value != 0) 515 1.1 christos { 516 1.1 christos PyErr_SetString (PyExc_AttributeError, 517 1.1 christos _("The value of `hit_count' must be zero.")); 518 1.1 christos return -1; 519 1.1 christos } 520 1.1 christos } 521 1.1 christos 522 1.1 christos self_bp->bp->hit_count = 0; 523 1.1 christos 524 1.1 christos return 0; 525 1.1 christos } 526 1.1 christos 527 1.1 christos /* Python function to get the location of a breakpoint. */ 528 1.1 christos static PyObject * 529 1.1 christos bppy_get_location (PyObject *self, void *closure) 530 1.1 christos { 531 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 532 1.1 christos 533 1.1 christos BPPY_REQUIRE_VALID (obj); 534 1.1 christos 535 1.10 christos if (obj->bp->type != bp_breakpoint 536 1.10 christos && obj->bp->type != bp_hardware_breakpoint) 537 1.1 christos Py_RETURN_NONE; 538 1.1 christos 539 1.10 christos const char *str = obj->bp->locspec->to_string (); 540 1.10 christos if (str == nullptr) 541 1.1 christos str = ""; 542 1.8 christos return host_string_to_python_string (str).release (); 543 1.1 christos } 544 1.1 christos 545 1.1 christos /* Python function to get the breakpoint expression. */ 546 1.1 christos static PyObject * 547 1.1 christos bppy_get_expression (PyObject *self, void *closure) 548 1.1 christos { 549 1.7 christos const char *str; 550 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 551 1.1 christos 552 1.1 christos BPPY_REQUIRE_VALID (obj); 553 1.1 christos 554 1.1 christos if (!is_watchpoint (obj->bp)) 555 1.1 christos Py_RETURN_NONE; 556 1.1 christos 557 1.11 christos watchpoint *wp = gdb::checked_static_cast<watchpoint *> (obj->bp); 558 1.1 christos 559 1.10 christos str = wp->exp_string.get (); 560 1.1 christos if (! str) 561 1.1 christos str = ""; 562 1.1 christos 563 1.8 christos return host_string_to_python_string (str).release (); 564 1.1 christos } 565 1.1 christos 566 1.1 christos /* Python function to get the condition expression of a breakpoint. */ 567 1.1 christos static PyObject * 568 1.1 christos bppy_get_condition (PyObject *self, void *closure) 569 1.1 christos { 570 1.1 christos char *str; 571 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 572 1.1 christos 573 1.1 christos BPPY_REQUIRE_VALID (obj); 574 1.1 christos 575 1.10 christos str = obj->bp->cond_string.get (); 576 1.1 christos if (! str) 577 1.1 christos Py_RETURN_NONE; 578 1.1 christos 579 1.8 christos return host_string_to_python_string (str).release (); 580 1.1 christos } 581 1.1 christos 582 1.1 christos /* Returns 0 on success. Returns -1 on error, with a python exception set. 583 1.1 christos */ 584 1.1 christos 585 1.1 christos static int 586 1.1 christos bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure) 587 1.1 christos { 588 1.7 christos gdb::unique_xmalloc_ptr<char> exp_holder; 589 1.7 christos const char *exp = NULL; 590 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 591 1.1 christos 592 1.1 christos BPPY_SET_REQUIRE_VALID (self_bp); 593 1.1 christos 594 1.1 christos if (newvalue == NULL) 595 1.1 christos { 596 1.1 christos PyErr_SetString (PyExc_TypeError, 597 1.1 christos _("Cannot delete `condition' attribute.")); 598 1.1 christos return -1; 599 1.1 christos } 600 1.1 christos else if (newvalue == Py_None) 601 1.1 christos exp = ""; 602 1.1 christos else 603 1.1 christos { 604 1.7 christos exp_holder = python_string_to_host_string (newvalue); 605 1.7 christos if (exp_holder == NULL) 606 1.1 christos return -1; 607 1.7 christos exp = exp_holder.get (); 608 1.1 christos } 609 1.1 christos 610 1.9 christos try 611 1.1 christos { 612 1.10 christos set_breakpoint_condition (self_bp->bp, exp, 0, false); 613 1.1 christos } 614 1.12 christos catch (const gdb_exception &ex) 615 1.5 christos { 616 1.12 christos return gdbpy_handle_gdb_exception (-1, ex); 617 1.5 christos } 618 1.1 christos 619 1.1 christos return 0; 620 1.1 christos } 621 1.1 christos 622 1.1 christos /* Python function to get the commands attached to a breakpoint. */ 623 1.1 christos static PyObject * 624 1.1 christos bppy_get_commands (PyObject *self, void *closure) 625 1.1 christos { 626 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 627 1.1 christos struct breakpoint *bp = self_bp->bp; 628 1.1 christos 629 1.1 christos BPPY_REQUIRE_VALID (self_bp); 630 1.1 christos 631 1.1 christos if (! self_bp->bp->commands) 632 1.1 christos Py_RETURN_NONE; 633 1.1 christos 634 1.7 christos string_file stb; 635 1.1 christos 636 1.9 christos try 637 1.1 christos { 638 1.10 christos ui_out_redirect_pop redir (current_uiout, &stb); 639 1.1 christos print_command_lines (current_uiout, breakpoint_commands (bp), 0); 640 1.1 christos } 641 1.9 christos catch (const gdb_exception &except) 642 1.1 christos { 643 1.12 christos return gdbpy_handle_gdb_exception (nullptr, except); 644 1.1 christos } 645 1.1 christos 646 1.8 christos return host_string_to_python_string (stb.c_str ()).release (); 647 1.8 christos } 648 1.8 christos 649 1.8 christos /* Set the commands attached to a breakpoint. Returns 0 on success. 650 1.8 christos Returns -1 on error, with a python exception set. */ 651 1.8 christos static int 652 1.8 christos bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure) 653 1.8 christos { 654 1.8 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 655 1.8 christos 656 1.8 christos BPPY_SET_REQUIRE_VALID (self_bp); 657 1.8 christos 658 1.8 christos gdb::unique_xmalloc_ptr<char> commands 659 1.8 christos (python_string_to_host_string (newvalue)); 660 1.8 christos if (commands == nullptr) 661 1.8 christos return -1; 662 1.8 christos 663 1.9 christos try 664 1.8 christos { 665 1.8 christos bool first = true; 666 1.8 christos char *save_ptr = nullptr; 667 1.8 christos auto reader 668 1.10 christos = [&] (std::string &buffer) 669 1.8 christos { 670 1.8 christos const char *result = strtok_r (first ? commands.get () : nullptr, 671 1.8 christos "\n", &save_ptr); 672 1.8 christos first = false; 673 1.8 christos return result; 674 1.8 christos }; 675 1.8 christos 676 1.8 christos counted_command_line lines = read_command_lines_1 (reader, 1, nullptr); 677 1.8 christos breakpoint_set_commands (self_bp->bp, std::move (lines)); 678 1.8 christos } 679 1.12 christos catch (const gdb_exception &ex) 680 1.8 christos { 681 1.12 christos return gdbpy_handle_gdb_exception (-1, ex); 682 1.8 christos } 683 1.8 christos 684 1.8 christos return 0; 685 1.1 christos } 686 1.1 christos 687 1.1 christos /* Python function to get the breakpoint type. */ 688 1.1 christos static PyObject * 689 1.1 christos bppy_get_type (PyObject *self, void *closure) 690 1.1 christos { 691 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 692 1.1 christos 693 1.1 christos BPPY_REQUIRE_VALID (self_bp); 694 1.1 christos 695 1.10 christos return gdb_py_object_from_longest (self_bp->bp->type).release (); 696 1.1 christos } 697 1.1 christos 698 1.1 christos /* Python function to get the visibility of the breakpoint. */ 699 1.1 christos 700 1.1 christos static PyObject * 701 1.1 christos bppy_get_visibility (PyObject *self, void *closure) 702 1.1 christos { 703 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 704 1.1 christos 705 1.1 christos BPPY_REQUIRE_VALID (self_bp); 706 1.1 christos 707 1.6 christos if (user_breakpoint_p (self_bp->bp)) 708 1.6 christos Py_RETURN_TRUE; 709 1.1 christos 710 1.6 christos Py_RETURN_FALSE; 711 1.1 christos } 712 1.1 christos 713 1.1 christos /* Python function to determine if the breakpoint is a temporary 714 1.1 christos breakpoint. */ 715 1.1 christos 716 1.1 christos static PyObject * 717 1.1 christos bppy_get_temporary (PyObject *self, void *closure) 718 1.1 christos { 719 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 720 1.1 christos 721 1.1 christos BPPY_REQUIRE_VALID (self_bp); 722 1.1 christos 723 1.1 christos if (self_bp->bp->disposition == disp_del 724 1.1 christos || self_bp->bp->disposition == disp_del_at_next_stop) 725 1.1 christos Py_RETURN_TRUE; 726 1.1 christos 727 1.1 christos Py_RETURN_FALSE; 728 1.1 christos } 729 1.1 christos 730 1.6 christos /* Python function to determine if the breakpoint is a pending 731 1.6 christos breakpoint. */ 732 1.6 christos 733 1.6 christos static PyObject * 734 1.6 christos bppy_get_pending (PyObject *self, void *closure) 735 1.6 christos { 736 1.6 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 737 1.6 christos 738 1.6 christos BPPY_REQUIRE_VALID (self_bp); 739 1.6 christos 740 1.6 christos if (is_watchpoint (self_bp->bp)) 741 1.6 christos Py_RETURN_FALSE; 742 1.6 christos if (pending_breakpoint_p (self_bp->bp)) 743 1.6 christos Py_RETURN_TRUE; 744 1.6 christos 745 1.6 christos Py_RETURN_FALSE; 746 1.6 christos } 747 1.6 christos 748 1.1 christos /* Python function to get the breakpoint's number. */ 749 1.1 christos static PyObject * 750 1.1 christos bppy_get_number (PyObject *self, void *closure) 751 1.1 christos { 752 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 753 1.1 christos 754 1.1 christos BPPY_REQUIRE_VALID (self_bp); 755 1.1 christos 756 1.10 christos return gdb_py_object_from_longest (self_bp->number).release (); 757 1.1 christos } 758 1.1 christos 759 1.1 christos /* Python function to get the breakpoint's thread ID. */ 760 1.1 christos static PyObject * 761 1.1 christos bppy_get_thread (PyObject *self, void *closure) 762 1.1 christos { 763 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 764 1.1 christos 765 1.1 christos BPPY_REQUIRE_VALID (self_bp); 766 1.1 christos 767 1.1 christos if (self_bp->bp->thread == -1) 768 1.1 christos Py_RETURN_NONE; 769 1.1 christos 770 1.10 christos return gdb_py_object_from_longest (self_bp->bp->thread).release (); 771 1.1 christos } 772 1.1 christos 773 1.11 christos /* Python function to get the breakpoint's inferior ID. */ 774 1.11 christos static PyObject * 775 1.11 christos bppy_get_inferior (PyObject *self, void *closure) 776 1.11 christos { 777 1.11 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 778 1.11 christos 779 1.11 christos BPPY_REQUIRE_VALID (self_bp); 780 1.11 christos 781 1.11 christos if (self_bp->bp->inferior == -1) 782 1.11 christos Py_RETURN_NONE; 783 1.11 christos 784 1.11 christos return gdb_py_object_from_longest (self_bp->bp->inferior).release (); 785 1.11 christos } 786 1.11 christos 787 1.1 christos /* Python function to get the breakpoint's task ID (in Ada). */ 788 1.1 christos static PyObject * 789 1.1 christos bppy_get_task (PyObject *self, void *closure) 790 1.1 christos { 791 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 792 1.1 christos 793 1.1 christos BPPY_REQUIRE_VALID (self_bp); 794 1.1 christos 795 1.11 christos if (self_bp->bp->task == -1) 796 1.1 christos Py_RETURN_NONE; 797 1.1 christos 798 1.10 christos return gdb_py_object_from_longest (self_bp->bp->task).release (); 799 1.1 christos } 800 1.1 christos 801 1.1 christos /* Python function to get the breakpoint's hit count. */ 802 1.1 christos static PyObject * 803 1.1 christos bppy_get_hit_count (PyObject *self, void *closure) 804 1.1 christos { 805 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 806 1.1 christos 807 1.1 christos BPPY_REQUIRE_VALID (self_bp); 808 1.1 christos 809 1.10 christos return gdb_py_object_from_longest (self_bp->bp->hit_count).release (); 810 1.1 christos } 811 1.1 christos 812 1.1 christos /* Python function to get the breakpoint's ignore count. */ 813 1.1 christos static PyObject * 814 1.1 christos bppy_get_ignore_count (PyObject *self, void *closure) 815 1.1 christos { 816 1.1 christos gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 817 1.1 christos 818 1.1 christos BPPY_REQUIRE_VALID (self_bp); 819 1.1 christos 820 1.10 christos return gdb_py_object_from_longest (self_bp->bp->ignore_count).release (); 821 1.10 christos } 822 1.10 christos 823 1.10 christos /* Python function to get the breakpoint locations of an owner breakpoint. */ 824 1.10 christos 825 1.10 christos static PyObject * 826 1.10 christos bppy_get_locations (PyObject *self, void *closure) 827 1.10 christos { 828 1.10 christos using py_bploc_t = gdbpy_breakpoint_location_object; 829 1.10 christos auto *self_bp = (gdbpy_breakpoint_object *) self; 830 1.10 christos BPPY_REQUIRE_VALID (self_bp); 831 1.10 christos 832 1.10 christos gdbpy_ref<> list (PyList_New (0)); 833 1.10 christos if (list == nullptr) 834 1.10 christos return nullptr; 835 1.10 christos 836 1.11 christos for (bp_location &loc : self_bp->bp->locations ()) 837 1.10 christos { 838 1.10 christos gdbpy_ref<py_bploc_t> py_bploc 839 1.10 christos (PyObject_New (py_bploc_t, &breakpoint_location_object_type)); 840 1.10 christos if (py_bploc == nullptr) 841 1.10 christos return nullptr; 842 1.10 christos 843 1.11 christos bp_location_ref_ptr ref = bp_location_ref_ptr::new_reference (&loc); 844 1.10 christos /* The location takes a reference to the owner breakpoint. 845 1.10 christos Decrements when they are de-allocated in bplocpy_dealloc */ 846 1.10 christos Py_INCREF (self); 847 1.10 christos py_bploc->owner = self_bp; 848 1.10 christos py_bploc->bp_loc = ref.release (); 849 1.10 christos if (PyList_Append (list.get (), (PyObject *) py_bploc.get ()) != 0) 850 1.10 christos return nullptr; 851 1.10 christos } 852 1.10 christos return list.release (); 853 1.1 christos } 854 1.1 christos 855 1.8 christos /* Internal function to validate the Python parameters/keywords 856 1.8 christos provided to bppy_init. */ 857 1.8 christos 858 1.8 christos static int 859 1.8 christos bppy_init_validate_args (const char *spec, char *source, 860 1.8 christos char *function, char *label, 861 1.8 christos char *line, enum bptype type) 862 1.8 christos { 863 1.8 christos /* If spec is defined, ensure that none of the explicit location 864 1.8 christos keywords are also defined. */ 865 1.8 christos if (spec != NULL) 866 1.8 christos { 867 1.8 christos if (source != NULL || function != NULL || label != NULL || line != NULL) 868 1.8 christos { 869 1.8 christos PyErr_SetString (PyExc_RuntimeError, 870 1.8 christos _("Breakpoints specified with spec cannot " 871 1.8 christos "have source, function, label or line defined.")); 872 1.8 christos return -1; 873 1.8 christos } 874 1.8 christos } 875 1.8 christos else 876 1.8 christos { 877 1.8 christos /* If spec isn't defined, ensure that the user is not trying to 878 1.8 christos define a watchpoint with an explicit location. */ 879 1.8 christos if (type == bp_watchpoint) 880 1.8 christos { 881 1.8 christos PyErr_SetString (PyExc_RuntimeError, 882 1.8 christos _("Watchpoints cannot be set by explicit " 883 1.8 christos "location parameters.")); 884 1.8 christos return -1; 885 1.8 christos } 886 1.8 christos else 887 1.8 christos { 888 1.8 christos /* Otherwise, ensure some explicit locations are defined. */ 889 1.8 christos if (source == NULL && function == NULL && label == NULL 890 1.8 christos && line == NULL) 891 1.8 christos { 892 1.8 christos PyErr_SetString (PyExc_RuntimeError, 893 1.8 christos _("Neither spec nor explicit location set.")); 894 1.8 christos return -1; 895 1.8 christos } 896 1.8 christos /* Finally, if source is specified, ensure that line, label 897 1.8 christos or function are specified too. */ 898 1.8 christos if (source != NULL && function == NULL && label == NULL 899 1.8 christos && line == NULL) 900 1.8 christos { 901 1.8 christos PyErr_SetString (PyExc_RuntimeError, 902 1.8 christos _("Specifying a source must also include a " 903 1.8 christos "line, label or function.")); 904 1.8 christos return -1; 905 1.8 christos } 906 1.8 christos } 907 1.8 christos } 908 1.8 christos return 1; 909 1.8 christos } 910 1.8 christos 911 1.1 christos /* Python function to create a new breakpoint. */ 912 1.1 christos static int 913 1.1 christos bppy_init (PyObject *self, PyObject *args, PyObject *kwargs) 914 1.1 christos { 915 1.7 christos static const char *keywords[] = { "spec", "type", "wp_class", "internal", 916 1.8 christos "temporary","source", "function", 917 1.8 christos "label", "line", "qualified", NULL }; 918 1.8 christos const char *spec = NULL; 919 1.8 christos enum bptype type = bp_breakpoint; 920 1.1 christos int access_type = hw_write; 921 1.1 christos PyObject *internal = NULL; 922 1.1 christos PyObject *temporary = NULL; 923 1.8 christos PyObject *lineobj = NULL;; 924 1.1 christos int internal_bp = 0; 925 1.1 christos int temporary_bp = 0; 926 1.8 christos gdb::unique_xmalloc_ptr<char> line; 927 1.8 christos char *label = NULL; 928 1.8 christos char *source = NULL; 929 1.8 christos char *function = NULL; 930 1.12 christos PyObject *qualified = Py_False; 931 1.1 christos 932 1.12 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO!", keywords, 933 1.7 christos &spec, &type, &access_type, 934 1.8 christos &internal, 935 1.8 christos &temporary, &source, 936 1.8 christos &function, &label, &lineobj, 937 1.12 christos &PyBool_Type, &qualified)) 938 1.1 christos return -1; 939 1.1 christos 940 1.8 christos 941 1.8 christos if (lineobj != NULL) 942 1.8 christos { 943 1.10 christos if (PyLong_Check (lineobj)) 944 1.10 christos line = xstrprintf ("%ld", PyLong_AsLong (lineobj)); 945 1.10 christos else if (PyUnicode_Check (lineobj)) 946 1.8 christos line = python_string_to_host_string (lineobj); 947 1.8 christos else 948 1.8 christos { 949 1.8 christos PyErr_SetString (PyExc_RuntimeError, 950 1.8 christos _("Line keyword should be an integer or a string. ")); 951 1.8 christos return -1; 952 1.8 christos } 953 1.8 christos } 954 1.8 christos 955 1.1 christos if (internal) 956 1.1 christos { 957 1.1 christos internal_bp = PyObject_IsTrue (internal); 958 1.1 christos if (internal_bp == -1) 959 1.1 christos return -1; 960 1.1 christos } 961 1.1 christos 962 1.1 christos if (temporary != NULL) 963 1.1 christos { 964 1.1 christos temporary_bp = PyObject_IsTrue (temporary); 965 1.1 christos if (temporary_bp == -1) 966 1.1 christos return -1; 967 1.1 christos } 968 1.1 christos 969 1.8 christos if (bppy_init_validate_args (spec, source, function, label, line.get (), 970 1.8 christos type) == -1) 971 1.8 christos return -1; 972 1.8 christos 973 1.1 christos bppy_pending_object = (gdbpy_breakpoint_object *) self; 974 1.1 christos bppy_pending_object->number = -1; 975 1.1 christos bppy_pending_object->bp = NULL; 976 1.1 christos 977 1.9 christos try 978 1.1 christos { 979 1.1 christos switch (type) 980 1.1 christos { 981 1.1 christos case bp_breakpoint: 982 1.10 christos case bp_hardware_breakpoint: 983 1.1 christos { 984 1.10 christos location_spec_up locspec; 985 1.12 christos gdb_assert (PyBool_Check (qualified)); 986 1.8 christos symbol_name_match_type func_name_match_type 987 1.12 christos = (qualified == Py_True 988 1.12 christos ? symbol_name_match_type::FULL 989 1.12 christos : symbol_name_match_type::WILD); 990 1.8 christos 991 1.8 christos if (spec != NULL) 992 1.8 christos { 993 1.8 christos gdb::unique_xmalloc_ptr<char> 994 1.8 christos copy_holder (xstrdup (skip_spaces (spec))); 995 1.8 christos const char *copy = copy_holder.get (); 996 1.8 christos 997 1.10 christos locspec = string_to_location_spec (©, 998 1.10 christos current_language, 999 1.10 christos func_name_match_type); 1000 1.8 christos } 1001 1.8 christos else 1002 1.8 christos { 1003 1.10 christos std::unique_ptr<explicit_location_spec> explicit_loc 1004 1.10 christos (new explicit_location_spec ()); 1005 1.8 christos 1006 1.11 christos if (source != nullptr) 1007 1.11 christos explicit_loc->source_filename = make_unique_xstrdup (source); 1008 1.11 christos if (function != nullptr) 1009 1.11 christos explicit_loc->function_name = make_unique_xstrdup (function); 1010 1.11 christos if (label != nullptr) 1011 1.11 christos explicit_loc->label_name = make_unique_xstrdup (label); 1012 1.8 christos 1013 1.8 christos if (line != NULL) 1014 1.10 christos explicit_loc->line_offset 1015 1.10 christos = linespec_parse_line_offset (line.get ()); 1016 1.8 christos 1017 1.10 christos explicit_loc->func_name_match_type = func_name_match_type; 1018 1.8 christos 1019 1.10 christos locspec.reset (explicit_loc.release ()); 1020 1.8 christos } 1021 1.8 christos 1022 1.10 christos const struct breakpoint_ops *ops 1023 1.10 christos = breakpoint_ops_for_location_spec (locspec.get (), false); 1024 1.9 christos 1025 1.10 christos create_breakpoint (gdbpy_enter::get_gdbarch (), 1026 1.11 christos locspec.get (), NULL, -1, -1, NULL, false, 1027 1.1 christos 0, 1028 1.10 christos temporary_bp, type, 1029 1.1 christos 0, 1030 1.1 christos AUTO_BOOLEAN_TRUE, 1031 1.9 christos ops, 1032 1.1 christos 0, 1, internal_bp, 0); 1033 1.1 christos break; 1034 1.1 christos } 1035 1.8 christos case bp_watchpoint: 1036 1.1 christos { 1037 1.10 christos spec = skip_spaces (spec); 1038 1.8 christos 1039 1.1 christos if (access_type == hw_write) 1040 1.10 christos watch_command_wrapper (spec, 0, internal_bp); 1041 1.1 christos else if (access_type == hw_access) 1042 1.10 christos awatch_command_wrapper (spec, 0, internal_bp); 1043 1.1 christos else if (access_type == hw_read) 1044 1.10 christos rwatch_command_wrapper (spec, 0, internal_bp); 1045 1.1 christos else 1046 1.1 christos error(_("Cannot understand watchpoint access type.")); 1047 1.1 christos break; 1048 1.1 christos } 1049 1.10 christos case bp_catchpoint: 1050 1.10 christos error (_("BP_CATCHPOINT not supported")); 1051 1.1 christos default: 1052 1.1 christos error(_("Do not understand breakpoint type to set.")); 1053 1.1 christos } 1054 1.1 christos } 1055 1.9 christos catch (const gdb_exception &except) 1056 1.1 christos { 1057 1.6 christos bppy_pending_object = NULL; 1058 1.12 christos return gdbpy_handle_gdb_exception (-1, except); 1059 1.1 christos } 1060 1.1 christos 1061 1.1 christos BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self); 1062 1.1 christos return 0; 1063 1.1 christos } 1064 1.1 christos 1065 1.11 christos /* __repr__ implementation for gdb.Breakpoint. */ 1066 1.11 christos 1067 1.11 christos static PyObject * 1068 1.11 christos bppy_repr (PyObject *self) 1069 1.11 christos { 1070 1.11 christos const auto bp = (struct gdbpy_breakpoint_object*) self; 1071 1.11 christos if (bp->bp == nullptr) 1072 1.11 christos return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name); 1073 1.11 christos 1074 1.11 christos std::string str = " "; 1075 1.11 christos if (bp->bp->thread != -1) 1076 1.11 christos str += string_printf ("thread=%d ", bp->bp->thread); 1077 1.11 christos if (bp->bp->task > 0) 1078 1.11 christos str += string_printf ("task=%d ", bp->bp->task); 1079 1.11 christos if (bp->bp->enable_count > 0) 1080 1.11 christos str += string_printf ("enable_count=%d ", bp->bp->enable_count); 1081 1.11 christos str.pop_back (); 1082 1.11 christos 1083 1.11 christos return PyUnicode_FromFormat ("<%s%s number=%d hits=%d%s>", 1084 1.11 christos Py_TYPE (self)->tp_name, 1085 1.11 christos (bp->bp->enable_state == bp_enabled 1086 1.11 christos ? "" : " disabled"), bp->bp->number, 1087 1.11 christos bp->bp->hit_count, str.c_str ()); 1088 1.11 christos } 1089 1.11 christos 1090 1.10 christos /* Append to LIST the breakpoint Python object associated to B. 1091 1.10 christos 1092 1.10 christos Return true on success. Return false on failure, with the Python error 1093 1.10 christos indicator set. */ 1094 1.1 christos 1095 1.9 christos static bool 1096 1.9 christos build_bp_list (struct breakpoint *b, PyObject *list) 1097 1.1 christos { 1098 1.1 christos PyObject *bp = (PyObject *) b->py_bp_object; 1099 1.1 christos 1100 1.1 christos /* Not all breakpoints will have a companion Python object. 1101 1.1 christos Only breakpoints that were created via bppy_new, or 1102 1.1 christos breakpoints that were created externally and are tracked by 1103 1.1 christos the Python Scripting API. */ 1104 1.10 christos if (bp == nullptr) 1105 1.10 christos return true; 1106 1.10 christos 1107 1.10 christos return PyList_Append (list, bp) == 0; 1108 1.10 christos } 1109 1.10 christos 1110 1.10 christos /* See python-internal.h. */ 1111 1.1 christos 1112 1.10 christos bool 1113 1.10 christos gdbpy_breakpoint_init_breakpoint_type () 1114 1.10 christos { 1115 1.10 christos if (breakpoint_object_type.tp_new == nullptr) 1116 1.10 christos { 1117 1.10 christos breakpoint_object_type.tp_new = PyType_GenericNew; 1118 1.12 christos if (gdbpy_type_ready (&breakpoint_object_type) < 0) 1119 1.10 christos { 1120 1.10 christos /* Reset tp_new back to nullptr so future calls to this function 1121 1.10 christos will try calling PyType_Ready again. */ 1122 1.10 christos breakpoint_object_type.tp_new = nullptr; 1123 1.10 christos return false; 1124 1.10 christos } 1125 1.10 christos } 1126 1.1 christos 1127 1.10 christos return true; 1128 1.1 christos } 1129 1.1 christos 1130 1.1 christos /* Static function to return a tuple holding all breakpoints. */ 1131 1.1 christos 1132 1.1 christos PyObject * 1133 1.1 christos gdbpy_breakpoints (PyObject *self, PyObject *args) 1134 1.1 christos { 1135 1.1 christos if (bppy_live == 0) 1136 1.6 christos return PyTuple_New (0); 1137 1.1 christos 1138 1.7 christos gdbpy_ref<> list (PyList_New (0)); 1139 1.7 christos if (list == NULL) 1140 1.1 christos return NULL; 1141 1.1 christos 1142 1.10 christos /* If build_bp_list returns false, it signals an error condition. In that 1143 1.10 christos case abandon building the list and return nullptr. */ 1144 1.11 christos for (breakpoint &bp : all_breakpoints ()) 1145 1.11 christos if (!build_bp_list (&bp, list.get ())) 1146 1.10 christos return nullptr; 1147 1.1 christos 1148 1.7 christos return PyList_AsTuple (list.get ()); 1149 1.1 christos } 1150 1.1 christos 1151 1.1 christos /* Call the "stop" method (if implemented) in the breakpoint 1152 1.1 christos class. If the method returns True, the inferior will be 1153 1.1 christos stopped at the breakpoint. Otherwise the inferior will be 1154 1.1 christos allowed to continue. */ 1155 1.1 christos 1156 1.3 christos enum ext_lang_bp_stop 1157 1.3 christos gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang, 1158 1.3 christos struct breakpoint *b) 1159 1.1 christos { 1160 1.3 christos int stop; 1161 1.3 christos struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object; 1162 1.3 christos PyObject *py_bp = (PyObject *) bp_obj; 1163 1.1 christos 1164 1.3 christos if (bp_obj == NULL) 1165 1.3 christos return EXT_LANG_BP_STOP_UNSET; 1166 1.3 christos 1167 1.3 christos stop = -1; 1168 1.7 christos 1169 1.10 christos gdbpy_enter enter_py (b->gdbarch); 1170 1.1 christos 1171 1.1 christos if (bp_obj->is_finish_bp) 1172 1.1 christos bpfinishpy_pre_stop_hook (bp_obj); 1173 1.1 christos 1174 1.1 christos if (PyObject_HasAttrString (py_bp, stop_func)) 1175 1.1 christos { 1176 1.12 christos gdbpy_ref<> result = gdbpy_call_method (py_bp, stop_func); 1177 1.1 christos 1178 1.3 christos stop = 1; 1179 1.7 christos if (result != NULL) 1180 1.1 christos { 1181 1.7 christos int evaluate = PyObject_IsTrue (result.get ()); 1182 1.1 christos 1183 1.1 christos if (evaluate == -1) 1184 1.1 christos gdbpy_print_stack (); 1185 1.1 christos 1186 1.1 christos /* If the "stop" function returns False that means 1187 1.1 christos the Python breakpoint wants GDB to continue. */ 1188 1.1 christos if (! evaluate) 1189 1.1 christos stop = 0; 1190 1.1 christos } 1191 1.1 christos else 1192 1.1 christos gdbpy_print_stack (); 1193 1.1 christos } 1194 1.1 christos 1195 1.1 christos if (bp_obj->is_finish_bp) 1196 1.1 christos bpfinishpy_post_stop_hook (bp_obj); 1197 1.1 christos 1198 1.3 christos if (stop < 0) 1199 1.3 christos return EXT_LANG_BP_STOP_UNSET; 1200 1.3 christos return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO; 1201 1.1 christos } 1202 1.1 christos 1203 1.1 christos /* Checks if the "stop" method exists in this breakpoint. 1204 1.1 christos Used by condition_command to ensure mutual exclusion of breakpoint 1205 1.1 christos conditions. */ 1206 1.1 christos 1207 1.1 christos int 1208 1.3 christos gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang, 1209 1.3 christos struct breakpoint *b) 1210 1.1 christos { 1211 1.3 christos PyObject *py_bp; 1212 1.3 christos 1213 1.3 christos if (b->py_bp_object == NULL) 1214 1.3 christos return 0; 1215 1.3 christos 1216 1.3 christos py_bp = (PyObject *) b->py_bp_object; 1217 1.1 christos 1218 1.10 christos gdbpy_enter enter_py (b->gdbarch); 1219 1.7 christos return PyObject_HasAttrString (py_bp, stop_func); 1220 1.1 christos } 1221 1.1 christos 1222 1.1 christos 1223 1.1 christos 1225 1.1 christos /* Event callback functions. */ 1226 1.1 christos 1227 1.1 christos /* Callback that is used when a breakpoint is created. This function 1228 1.1 christos will create a new Python breakpoint object. */ 1229 1.1 christos static void 1230 1.1 christos gdbpy_breakpoint_created (struct breakpoint *bp) 1231 1.10 christos { 1232 1.10 christos PYBP_SCOPED_DEBUG_ENTER_EXIT; 1233 1.1 christos 1234 1.1 christos gdbpy_breakpoint_object *newbp; 1235 1.6 christos 1236 1.10 christos if (!user_breakpoint_p (bp) && bppy_pending_object == NULL) 1237 1.10 christos { 1238 1.10 christos pybp_debug_printf ("not attaching python object to this breakpoint"); 1239 1.10 christos return; 1240 1.1 christos } 1241 1.1 christos 1242 1.10 christos if (bp->type != bp_breakpoint 1243 1.1 christos && bp->type != bp_hardware_breakpoint 1244 1.1 christos && bp->type != bp_watchpoint 1245 1.1 christos && bp->type != bp_hardware_watchpoint 1246 1.10 christos && bp->type != bp_read_watchpoint 1247 1.10 christos && bp->type != bp_access_watchpoint 1248 1.10 christos && bp->type != bp_catchpoint) 1249 1.10 christos { 1250 1.10 christos pybp_debug_printf ("is not a breakpoint or watchpoint"); 1251 1.10 christos return; 1252 1.1 christos } 1253 1.10 christos 1254 1.1 christos gdbpy_enter enter_py (bp->gdbarch); 1255 1.1 christos 1256 1.1 christos if (bppy_pending_object) 1257 1.1 christos { 1258 1.9 christos newbp = bppy_pending_object; 1259 1.1 christos Py_INCREF (newbp); 1260 1.10 christos bppy_pending_object = NULL; 1261 1.1 christos pybp_debug_printf ("attaching existing breakpoint object"); 1262 1.1 christos } 1263 1.10 christos else 1264 1.10 christos { 1265 1.10 christos newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type); 1266 1.10 christos pybp_debug_printf ("attaching new breakpoint object"); 1267 1.1 christos } 1268 1.1 christos if (newbp) 1269 1.1 christos { 1270 1.1 christos newbp->number = bp->number; 1271 1.1 christos newbp->bp = bp; 1272 1.1 christos newbp->bp->py_bp_object = newbp; 1273 1.1 christos newbp->is_finish_bp = 0; 1274 1.1 christos ++bppy_live; 1275 1.1 christos } 1276 1.1 christos else 1277 1.1 christos { 1278 1.1 christos PyErr_SetString (PyExc_RuntimeError, 1279 1.1 christos _("Error while creating breakpoint from GDB.")); 1280 1.1 christos gdbpy_print_stack (); 1281 1.1 christos } 1282 1.6 christos 1283 1.6 christos if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created)) 1284 1.6 christos { 1285 1.6 christos if (evpy_emit_event ((PyObject *) newbp, 1286 1.6 christos gdb_py_events.breakpoint_created) < 0) 1287 1.6 christos gdbpy_print_stack (); 1288 1.1 christos } 1289 1.1 christos } 1290 1.1 christos 1291 1.1 christos /* Callback that is used when a breakpoint is deleted. This will 1292 1.1 christos invalidate the corresponding Python object. */ 1293 1.1 christos static void 1294 1.1 christos gdbpy_breakpoint_deleted (struct breakpoint *b) 1295 1.10 christos { 1296 1.10 christos PYBP_SCOPED_DEBUG_ENTER_EXIT; 1297 1.1 christos 1298 1.1 christos int num = b->number; 1299 1.1 christos struct breakpoint *bp = NULL; 1300 1.1 christos 1301 1.1 christos bp = get_breakpoint (num); 1302 1.1 christos if (bp) 1303 1.10 christos { 1304 1.9 christos gdbpy_enter enter_py (b->gdbarch); 1305 1.7 christos 1306 1.7 christos gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object); 1307 1.1 christos if (bp_obj != NULL) 1308 1.11 christos { 1309 1.11 christos if (bp_obj->is_finish_bp) 1310 1.11 christos bpfinishpy_pre_delete_hook (bp_obj.get ()); 1311 1.6 christos 1312 1.6 christos if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted)) 1313 1.7 christos { 1314 1.6 christos if (evpy_emit_event ((PyObject *) bp_obj.get (), 1315 1.6 christos gdb_py_events.breakpoint_deleted) < 0) 1316 1.6 christos gdbpy_print_stack (); 1317 1.6 christos } 1318 1.1 christos 1319 1.1 christos bp_obj->bp = NULL; 1320 1.1 christos --bppy_live; 1321 1.1 christos } 1322 1.1 christos } 1323 1.1 christos } 1324 1.6 christos 1325 1.6 christos /* Callback that is used when a breakpoint is modified. */ 1326 1.6 christos 1327 1.6 christos static void 1328 1.6 christos gdbpy_breakpoint_modified (struct breakpoint *b) 1329 1.10 christos { 1330 1.10 christos PYBP_SCOPED_DEBUG_ENTER_EXIT; 1331 1.6 christos 1332 1.6 christos int num = b->number; 1333 1.6 christos struct breakpoint *bp = NULL; 1334 1.6 christos 1335 1.6 christos bp = get_breakpoint (num); 1336 1.6 christos if (bp) 1337 1.10 christos { 1338 1.9 christos gdbpy_enter enter_py (b->gdbarch); 1339 1.6 christos 1340 1.6 christos PyObject *bp_obj = (PyObject *) bp->py_bp_object; 1341 1.6 christos if (bp_obj) 1342 1.6 christos { 1343 1.6 christos if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified)) 1344 1.6 christos { 1345 1.6 christos if (evpy_emit_event (bp_obj, 1346 1.6 christos gdb_py_events.breakpoint_modified) < 0) 1347 1.6 christos gdbpy_print_stack (); 1348 1.6 christos } 1349 1.6 christos } 1350 1.6 christos } 1351 1.6 christos } 1352 1.1 christos 1353 1.1 christos 1354 1.1 christos 1356 1.1 christos /* Initialize the Python breakpoint code. */ 1357 1.1 christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION 1358 1.1 christos gdbpy_initialize_breakpoints (void) 1359 1.1 christos { 1360 1.10 christos int i; 1361 1.1 christos 1362 1.1 christos if (!gdbpy_breakpoint_init_breakpoint_type ()) 1363 1.10 christos return -1; 1364 1.10 christos 1365 1.10 christos gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created, 1366 1.10 christos "py-breakpoint"); 1367 1.10 christos gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted, 1368 1.10 christos "py-breakpoint"); 1369 1.1 christos gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified, 1370 1.1 christos "py-breakpoint"); 1371 1.1 christos 1372 1.1 christos /* Add breakpoint types constants. */ 1373 1.9 christos for (i = 0; pybp_codes[i].name; ++i) 1374 1.1 christos { 1375 1.1 christos if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name, 1376 1.1 christos pybp_codes[i].code) < 0) 1377 1.1 christos return -1; 1378 1.1 christos } 1379 1.1 christos 1380 1.1 christos /* Add watchpoint types constants. */ 1381 1.9 christos for (i = 0; pybp_watch_types[i].name; ++i) 1382 1.1 christos { 1383 1.1 christos if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name, 1384 1.1 christos pybp_watch_types[i].code) < 0) 1385 1.1 christos return -1; 1386 1.1 christos } 1387 1.1 christos 1388 1.1 christos return 0; 1389 1.10 christos } 1390 1.10 christos 1391 1.11 christos /* Initialize the Python BreakpointLocation code. */ 1392 1.10 christos 1393 1.10 christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION 1394 1.12 christos gdbpy_initialize_breakpoint_locations () 1395 1.10 christos { 1396 1.10 christos return gdbpy_type_ready (&breakpoint_location_object_type); 1397 1.1 christos } 1398 1.1 christos 1399 1.1 christos 1400 1.1 christos 1402 1.1 christos /* Helper function that overrides this Python object's 1403 1.1 christos PyObject_GenericSetAttr to allow extra validation of the attribute 1404 1.1 christos being set. */ 1405 1.1 christos 1406 1.1 christos static int 1407 1.7 christos local_setattro (PyObject *self, PyObject *name, PyObject *v) 1408 1.1 christos { 1409 1.1 christos gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 1410 1.1 christos gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name)); 1411 1.1 christos 1412 1.1 christos if (attr == NULL) 1413 1.3 christos return -1; 1414 1.3 christos 1415 1.7 christos /* If the attribute trying to be set is the "stop" method, 1416 1.3 christos but we already have a condition set in the CLI or other extension 1417 1.3 christos language, disallow this operation. */ 1418 1.3 christos if (strcmp (attr.get (), stop_func) == 0) 1419 1.3 christos { 1420 1.3 christos const struct extension_language_defn *extlang = NULL; 1421 1.3 christos 1422 1.3 christos if (obj->bp->cond_string != NULL) 1423 1.3 christos extlang = get_ext_lang_defn (EXT_LANG_GDB); 1424 1.3 christos if (extlang == NULL) 1425 1.8 christos extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON); 1426 1.8 christos if (extlang != NULL) 1427 1.8 christos { 1428 1.8 christos std::string error_text 1429 1.8 christos = string_printf (_("Only one stop condition allowed. There is" 1430 1.8 christos " currently a %s stop condition defined for" 1431 1.3 christos " this breakpoint."), 1432 1.3 christos ext_lang_capitalized_name (extlang)); 1433 1.1 christos PyErr_SetString (PyExc_RuntimeError, error_text.c_str ()); 1434 1.1 christos return -1; 1435 1.8 christos } 1436 1.1 christos } 1437 1.1 christos 1438 1.7 christos return PyObject_GenericSetAttr (self, name, v); 1439 1.1 christos } 1440 1.1 christos 1441 1.1 christos static gdb_PyGetSetDef breakpoint_object_getset[] = { 1442 1.1 christos { "enabled", bppy_get_enabled, bppy_set_enabled, 1443 1.1 christos "Boolean telling whether the breakpoint is enabled.", NULL }, 1444 1.1 christos { "silent", bppy_get_silent, bppy_set_silent, 1445 1.1 christos "Boolean telling whether the breakpoint is silent.", NULL }, 1446 1.1 christos { "thread", bppy_get_thread, bppy_set_thread, 1447 1.1 christos "Thread ID for the breakpoint.\n\ 1448 1.11 christos If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\ 1449 1.11 christos If the value is None, then this breakpoint is not thread-specific.\n\ 1450 1.11 christos No other type of value can be used.", NULL }, 1451 1.11 christos { "inferior", bppy_get_inferior, bppy_set_inferior, 1452 1.11 christos "Inferior ID for the breakpoint.\n\ 1453 1.1 christos If the value is an inferior ID (integer), then this is an inferior-specific\n\ 1454 1.1 christos breakpoint. If the value is None, then this breakpoint is not\n\ 1455 1.1 christos inferior-specific. No other type of value can be used.", NULL }, 1456 1.1 christos { "task", bppy_get_task, bppy_set_task, 1457 1.1 christos "Thread ID for the breakpoint.\n\ 1458 1.1 christos If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\ 1459 1.1 christos If the value is None, then this breakpoint is not task-specific.\n\ 1460 1.1 christos No other type of value can be used.", NULL }, 1461 1.1 christos { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count, 1462 1.1 christos "Number of times this breakpoint should be automatically continued.", 1463 1.1 christos NULL }, 1464 1.1 christos { "number", bppy_get_number, NULL, 1465 1.1 christos "Breakpoint's number assigned by GDB.", NULL }, 1466 1.1 christos { "hit_count", bppy_get_hit_count, bppy_set_hit_count, 1467 1.1 christos "Number of times the breakpoint has been hit.\n\ 1468 1.1 christos Can be set to zero to clear the count. No other value is valid\n\ 1469 1.1 christos when setting this property.", NULL }, 1470 1.1 christos { "location", bppy_get_location, NULL, 1471 1.1 christos "Location of the breakpoint, as specified by the user.", NULL}, 1472 1.1 christos { "expression", bppy_get_expression, NULL, 1473 1.1 christos "Expression of the breakpoint, as specified by the user.", NULL}, 1474 1.8 christos { "condition", bppy_get_condition, bppy_set_condition, 1475 1.1 christos "Condition of the breakpoint, as specified by the user,\ 1476 1.1 christos or None if no condition set."}, 1477 1.1 christos { "commands", bppy_get_commands, bppy_set_commands, 1478 1.1 christos "Commands of the breakpoint, as specified by the user."}, 1479 1.1 christos { "type", bppy_get_type, NULL, 1480 1.1 christos "Type of breakpoint."}, 1481 1.1 christos { "visible", bppy_get_visibility, NULL, 1482 1.6 christos "Whether the breakpoint is visible to the user."}, 1483 1.6 christos { "temporary", bppy_get_temporary, NULL, 1484 1.10 christos "Whether this breakpoint is a temporary breakpoint."}, 1485 1.10 christos { "pending", bppy_get_pending, NULL, 1486 1.1 christos "Whether this breakpoint is a pending breakpoint."}, 1487 1.1 christos { "locations", bppy_get_locations, NULL, 1488 1.1 christos "Get locations where this breakpoint was set"}, 1489 1.1 christos { NULL } /* Sentinel. */ 1490 1.1 christos }; 1491 1.1 christos 1492 1.1 christos static PyMethodDef breakpoint_object_methods[] = 1493 1.1 christos { 1494 1.1 christos { "is_valid", bppy_is_valid, METH_NOARGS, 1495 1.1 christos "Return true if this breakpoint is valid, false if not." }, 1496 1.1 christos { "delete", bppy_delete_breakpoint, METH_NOARGS, 1497 1.1 christos "Delete the underlying GDB breakpoint." }, 1498 1.1 christos { NULL } /* Sentinel. */ 1499 1.1 christos }; 1500 1.1 christos 1501 1.1 christos PyTypeObject breakpoint_object_type = 1502 1.1 christos { 1503 1.1 christos PyVarObject_HEAD_INIT (NULL, 0) 1504 1.1 christos "gdb.Breakpoint", /*tp_name*/ 1505 1.1 christos sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/ 1506 1.1 christos 0, /*tp_itemsize*/ 1507 1.1 christos 0, /*tp_dealloc*/ 1508 1.1 christos 0, /*tp_print*/ 1509 1.11 christos 0, /*tp_getattr*/ 1510 1.1 christos 0, /*tp_setattr*/ 1511 1.1 christos 0, /*tp_compare*/ 1512 1.1 christos bppy_repr, /*tp_repr*/ 1513 1.1 christos 0, /*tp_as_number*/ 1514 1.1 christos 0, /*tp_as_sequence*/ 1515 1.1 christos 0, /*tp_as_mapping*/ 1516 1.1 christos 0, /*tp_hash */ 1517 1.1 christos 0, /*tp_call*/ 1518 1.1 christos 0, /*tp_str*/ 1519 1.1 christos 0, /*tp_getattro*/ 1520 1.1 christos (setattrofunc)local_setattro, /*tp_setattro */ 1521 1.1 christos 0, /*tp_as_buffer*/ 1522 1.1 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 1523 1.1 christos "GDB breakpoint object", /* tp_doc */ 1524 1.1 christos 0, /* tp_traverse */ 1525 1.1 christos 0, /* tp_clear */ 1526 1.1 christos 0, /* tp_richcompare */ 1527 1.1 christos 0, /* tp_weaklistoffset */ 1528 1.1 christos 0, /* tp_iter */ 1529 1.1 christos 0, /* tp_iternext */ 1530 1.1 christos breakpoint_object_methods, /* tp_methods */ 1531 1.1 christos 0, /* tp_members */ 1532 1.1 christos breakpoint_object_getset, /* tp_getset */ 1533 1.1 christos 0, /* tp_base */ 1534 1.1 christos 0, /* tp_dict */ 1535 1.1 christos 0, /* tp_descr_get */ 1536 1.1 christos 0, /* tp_descr_set */ 1537 1.1 christos 0, /* tp_dictoffset */ 1538 1.10 christos bppy_init, /* tp_init */ 1539 1.10 christos 0, /* tp_alloc */ 1540 1.10 christos }; 1541 1.10 christos 1542 1.10 christos void _initialize_py_breakpoint (); 1543 1.10 christos void 1544 1.10 christos _initialize_py_breakpoint () 1545 1.10 christos { 1546 1.10 christos add_setshow_boolean_cmd 1547 1.10 christos ("py-breakpoint", class_maintenance, &pybp_debug, 1548 1.10 christos _("Set Python breakpoint debugging."), 1549 1.10 christos _("Show Python breakpoint debugging."), 1550 1.10 christos _("When on, Python breakpoint debugging is enabled."), 1551 1.10 christos NULL, 1552 1.10 christos show_pybp_debug, 1553 1.11 christos &setdebuglist, &showdebuglist); 1554 1.11 christos } 1555 1.11 christos 1556 1.10 christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_breakpoints); 1557 1.10 christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_breakpoint_locations); 1558 1.10 christos 1559 1.10 christos /* Python function to set the enabled state of a breakpoint location. */ 1560 1.10 christos 1561 1.10 christos static int 1562 1.10 christos bplocpy_set_enabled (PyObject *py_self, PyObject *newvalue, void *closure) 1563 1.10 christos { 1564 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1565 1.10 christos BPPY_SET_REQUIRE_VALID (self->owner); 1566 1.10 christos BPLOCPY_SET_REQUIRE_VALID (self->owner, self); 1567 1.10 christos 1568 1.10 christos if (newvalue == nullptr) 1569 1.10 christos { 1570 1.10 christos PyErr_SetString (PyExc_TypeError, 1571 1.10 christos _("Cannot delete 'enabled' attribute.")); 1572 1.10 christos return -1; 1573 1.10 christos } 1574 1.10 christos else if (!PyBool_Check (newvalue)) 1575 1.10 christos { 1576 1.10 christos PyErr_SetString (PyExc_TypeError, 1577 1.10 christos _("The value of 'enabled' must be a boolean.")); 1578 1.10 christos return -1; 1579 1.10 christos } 1580 1.10 christos 1581 1.10 christos int cmp = PyObject_IsTrue (newvalue); 1582 1.10 christos if (cmp < 0) 1583 1.10 christos return -1; 1584 1.10 christos 1585 1.10 christos try 1586 1.10 christos { 1587 1.10 christos enable_disable_bp_location (self->bp_loc, cmp == 1); 1588 1.12 christos } 1589 1.10 christos catch (const gdb_exception &except) 1590 1.10 christos { 1591 1.10 christos return gdbpy_handle_gdb_exception (-1, except); 1592 1.10 christos } 1593 1.10 christos return 0; 1594 1.10 christos } 1595 1.10 christos 1596 1.10 christos /* Python function to test whether or not the breakpoint location is enabled. */ 1597 1.10 christos 1598 1.10 christos static PyObject * 1599 1.10 christos bplocpy_get_enabled (PyObject *py_self, void *closure) 1600 1.10 christos { 1601 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1602 1.10 christos BPPY_REQUIRE_VALID (self->owner); 1603 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1604 1.10 christos 1605 1.10 christos if (self->bp_loc->enabled) 1606 1.10 christos Py_RETURN_TRUE; 1607 1.10 christos else 1608 1.10 christos Py_RETURN_FALSE; 1609 1.10 christos } 1610 1.10 christos 1611 1.10 christos /* Python function to get address of breakpoint location. */ 1612 1.10 christos 1613 1.10 christos static PyObject * 1614 1.10 christos bplocpy_get_address (PyObject *py_self, void *closure) 1615 1.10 christos { 1616 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1617 1.10 christos BPPY_REQUIRE_VALID (self->owner); 1618 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1619 1.10 christos return gdb_py_object_from_ulongest (self->bp_loc->address).release (); 1620 1.10 christos } 1621 1.10 christos 1622 1.10 christos /* Python function to get owner of breakpoint location, which 1623 1.10 christos is of type gdb.Breakpoint. */ 1624 1.10 christos 1625 1.10 christos static PyObject * 1626 1.10 christos bplocpy_get_owner (PyObject *py_self, void *closure) 1627 1.10 christos { 1628 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1629 1.10 christos BPPY_REQUIRE_VALID (self->owner); 1630 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1631 1.10 christos Py_INCREF (self->owner); 1632 1.10 christos return (PyObject *) self->owner; 1633 1.10 christos } 1634 1.10 christos 1635 1.10 christos /* Python function to get the source file name path and line number 1636 1.10 christos where this breakpoint location was set. */ 1637 1.10 christos 1638 1.10 christos static PyObject * 1639 1.10 christos bplocpy_get_source_location (PyObject *py_self, void *closure) 1640 1.10 christos { 1641 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1642 1.10 christos BPPY_REQUIRE_VALID (self->owner); 1643 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1644 1.10 christos if (self->bp_loc->symtab) 1645 1.10 christos { 1646 1.10 christos gdbpy_ref<> tup (PyTuple_New (2)); 1647 1.10 christos if (tup == nullptr) 1648 1.10 christos return nullptr; 1649 1.10 christos /* symtab->filename is never NULL. */ 1650 1.10 christos gdbpy_ref<> filename 1651 1.10 christos = host_string_to_python_string (self->bp_loc->symtab->filename); 1652 1.10 christos if (filename == nullptr) 1653 1.10 christos return nullptr; 1654 1.10 christos auto line = gdb_py_object_from_ulongest (self->bp_loc->line_number); 1655 1.10 christos if (line == nullptr) 1656 1.10 christos return nullptr; 1657 1.10 christos if (PyTuple_SetItem (tup.get (), 0, filename.release ()) == -1 1658 1.10 christos || PyTuple_SetItem (tup.get (), 1, line.release ()) == -1) 1659 1.10 christos return nullptr; 1660 1.10 christos return tup.release (); 1661 1.10 christos } 1662 1.10 christos else 1663 1.10 christos Py_RETURN_NONE; 1664 1.10 christos } 1665 1.10 christos 1666 1.10 christos /* Python function to get the function name of where this location was set. */ 1667 1.10 christos 1668 1.10 christos static PyObject * 1669 1.10 christos bplocpy_get_function (PyObject *py_self, void *closure) 1670 1.10 christos { 1671 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1672 1.10 christos BPPY_REQUIRE_VALID (self->owner); 1673 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1674 1.10 christos const auto fn_name = self->bp_loc->function_name.get (); 1675 1.10 christos if (fn_name != nullptr) 1676 1.10 christos return host_string_to_python_string (fn_name).release (); 1677 1.10 christos Py_RETURN_NONE; 1678 1.10 christos } 1679 1.10 christos 1680 1.10 christos static PyObject * 1681 1.10 christos bplocpy_get_thread_groups (PyObject *py_self, void *closure) 1682 1.10 christos { 1683 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1684 1.10 christos BPPY_REQUIRE_VALID (self->owner); 1685 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1686 1.10 christos gdbpy_ref<> list (PyList_New (0)); 1687 1.10 christos if (list == nullptr) 1688 1.10 christos return nullptr; 1689 1.10 christos for (inferior *inf : all_inferiors ()) 1690 1.10 christos { 1691 1.10 christos if (inf->pspace == self->bp_loc->pspace) 1692 1.10 christos { 1693 1.12 christos gdbpy_ref<> num = gdb_py_object_from_ulongest (inf->num); 1694 1.10 christos if (num == nullptr) 1695 1.10 christos return nullptr; 1696 1.10 christos if (PyList_Append (list.get (), num.get ()) != 0) 1697 1.10 christos return nullptr; 1698 1.10 christos } 1699 1.10 christos } 1700 1.10 christos return list.release (); 1701 1.10 christos } 1702 1.10 christos 1703 1.10 christos static PyObject * 1704 1.10 christos bplocpy_get_fullname (PyObject *py_self, void *closure) 1705 1.10 christos { 1706 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1707 1.12 christos BPPY_REQUIRE_VALID (self->owner); 1708 1.10 christos BPLOCPY_REQUIRE_VALID (self->owner, self); 1709 1.10 christos const auto symtab = self->bp_loc->symtab; 1710 1.12 christos if (symtab != nullptr && symtab->fullname () != nullptr) 1711 1.10 christos { 1712 1.10 christos gdbpy_ref<> fullname 1713 1.10 christos = host_string_to_python_string (symtab->fullname ()); 1714 1.10 christos return fullname.release (); 1715 1.10 christos } 1716 1.10 christos Py_RETURN_NONE; 1717 1.10 christos } 1718 1.10 christos 1719 1.10 christos /* De-allocation function to be called for the Python object. */ 1720 1.10 christos 1721 1.10 christos static void 1722 1.10 christos bplocpy_dealloc (PyObject *py_self) 1723 1.10 christos { 1724 1.10 christos auto *self = (gdbpy_breakpoint_location_object *) py_self; 1725 1.10 christos bp_location_ref_ptr decrementing_ref {self->bp_loc}; 1726 1.10 christos Py_XDECREF (self->owner); 1727 1.11 christos Py_TYPE (py_self)->tp_free (py_self); 1728 1.11 christos } 1729 1.11 christos 1730 1.11 christos /* __repr__ implementation for gdb.BreakpointLocation. */ 1731 1.11 christos 1732 1.11 christos static PyObject * 1733 1.11 christos bplocpy_repr (PyObject *py_self) 1734 1.11 christos { 1735 1.11 christos const auto self = (gdbpy_breakpoint_location_object *) py_self; 1736 1.11 christos if (self->owner == nullptr || self->owner->bp == nullptr 1737 1.11 christos || self->owner->bp != self->bp_loc->owner) 1738 1.11 christos return gdb_py_invalid_object_repr (py_self); 1739 1.11 christos 1740 1.11 christos const auto enabled = self->bp_loc->enabled ? "enabled" : "disabled"; 1741 1.11 christos 1742 1.11 christos std::string str (enabled); 1743 1.11 christos 1744 1.11 christos str += string_printf (" address=%s", 1745 1.11 christos paddress (self->bp_loc->owner->gdbarch, 1746 1.11 christos self->bp_loc->address)); 1747 1.11 christos 1748 1.11 christos if (self->bp_loc->requested_address != self->bp_loc->address) 1749 1.11 christos str += string_printf (" requested_address=%s", 1750 1.11 christos paddress (self->bp_loc->owner->gdbarch, 1751 1.11 christos self->bp_loc->requested_address)); 1752 1.11 christos if (self->bp_loc->symtab != nullptr) 1753 1.11 christos str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename, 1754 1.11 christos self->bp_loc->line_number); 1755 1.11 christos 1756 1.11 christos const auto fn_name = self->bp_loc->function_name.get (); 1757 1.11 christos if (fn_name != nullptr) 1758 1.11 christos { 1759 1.11 christos str += " in "; 1760 1.11 christos str += fn_name; 1761 1.11 christos } 1762 1.11 christos 1763 1.11 christos return PyUnicode_FromFormat ("<%s %s>", Py_TYPE (self)->tp_name, 1764 1.10 christos str.c_str ()); 1765 1.10 christos } 1766 1.10 christos 1767 1.10 christos /* Attribute get/set Python definitions. */ 1768 1.10 christos 1769 1.10 christos static gdb_PyGetSetDef bp_location_object_getset[] = { 1770 1.10 christos { "enabled", bplocpy_get_enabled, bplocpy_set_enabled, 1771 1.10 christos "Boolean telling whether the breakpoint is enabled.", NULL }, 1772 1.10 christos { "owner", bplocpy_get_owner, NULL, 1773 1.10 christos "Get the breakpoint owner object", NULL }, 1774 1.10 christos { "address", bplocpy_get_address, NULL, 1775 1.10 christos "Get address of where this location was set", NULL}, 1776 1.10 christos { "source", bplocpy_get_source_location, NULL, 1777 1.10 christos "Get file and line number of where this location was set", NULL}, 1778 1.10 christos { "function", bplocpy_get_function, NULL, 1779 1.10 christos "Get function of where this location was set", NULL }, 1780 1.10 christos { "fullname", bplocpy_get_fullname, NULL, 1781 1.10 christos "Get fullname of where this location was set", NULL }, 1782 1.10 christos { "thread_groups", bplocpy_get_thread_groups, NULL, 1783 1.10 christos "Get thread groups where this location is in", NULL }, 1784 1.10 christos { NULL } /* Sentinel. */ 1785 1.10 christos }; 1786 1.10 christos 1787 1.10 christos PyTypeObject breakpoint_location_object_type = 1788 1.10 christos { 1789 1.10 christos PyVarObject_HEAD_INIT (NULL, 0) 1790 1.10 christos "gdb.BreakpointLocation", /*tp_name*/ 1791 1.10 christos sizeof (gdbpy_breakpoint_location_object), /*tp_basicsize*/ 1792 1.10 christos 0, /*tp_itemsize*/ 1793 1.10 christos bplocpy_dealloc, /*tp_dealloc*/ 1794 1.10 christos 0, /*tp_print*/ 1795 1.11 christos 0, /*tp_getattr*/ 1796 1.10 christos 0, /*tp_setattr*/ 1797 1.10 christos 0, /*tp_compare*/ 1798 1.10 christos bplocpy_repr, /*tp_repr*/ 1799 1.10 christos 0, /*tp_as_number*/ 1800 1.10 christos 0, /*tp_as_sequence*/ 1801 1.10 christos 0, /*tp_as_mapping*/ 1802 1.10 christos 0, /*tp_hash */ 1803 1.10 christos 0, /*tp_call*/ 1804 1.10 christos 0, /*tp_str*/ 1805 1.10 christos 0, /*tp_getattro*/ 1806 1.10 christos 0, /*tp_setattro */ 1807 1.10 christos 0, /*tp_as_buffer*/ 1808 1.10 christos Py_TPFLAGS_DEFAULT, /*tp_flags*/ 1809 1.10 christos "GDB breakpoint location object", /* tp_doc */ 1810 1.10 christos 0, /* tp_traverse */ 1811 1.10 christos 0, /* tp_clear */ 1812 1.10 christos 0, /* tp_richcompare */ 1813 1.10 christos 0, /* tp_weaklistoffset */ 1814 1.10 christos 0, /* tp_iter */ 1815 1.10 christos 0, /* tp_iternext */ 1816 1.10 christos 0, /* tp_methods */ 1817 1.10 christos 0, /* tp_members */ 1818 1.10 christos bp_location_object_getset, /* tp_getset */ 1819 1.10 christos 0, /* tp_base */ 1820 1.10 christos 0, /* tp_dict */ 1821 1.10 christos 0, /* tp_descr_get */ 1822 1.10 christos 0, /* tp_descr_set */ 1823 1.10 christos 0, /* tp_dictoffset */ 1824 0, /* tp_init */ 1825 0, /* tp_alloc */ 1826 }; 1827