py-event.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Python interface to inferior events.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "py-event.h"
  16. void
  17. evpy_dealloc (PyObject *self)
  18. {
  19. Py_XDECREF (((event_object *) self)->dict);
  20. Py_TYPE (self)->tp_free (self);
  21. }
  22. gdbpy_ref<>
  23. create_event_object (PyTypeObject *py_type)
  24. {
  25. gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
  26. if (event_obj == NULL)
  27. return NULL;
  28. event_obj->dict = PyDict_New ();
  29. if (!event_obj->dict)
  30. return NULL;
  31. return gdbpy_ref<> ((PyObject *) event_obj.release ());
  32. }
  33. /* Add the attribute ATTR to the event object EVENT. In
  34. python this attribute will be accessible by the name NAME.
  35. returns 0 if the operation succeeds and -1 otherwise. This
  36. function acquires a new reference to ATTR. */
  37. int
  38. evpy_add_attribute (PyObject *event, const char *name, PyObject *attr)
  39. {
  40. return PyObject_SetAttrString (event, name, attr);
  41. }
  42. /* Initialize the Python event code. */
  43. int
  44. gdbpy_initialize_event (void)
  45. {
  46. return gdbpy_initialize_event_generic (&event_object_type,
  47. "Event");
  48. }
  49. /* Initialize the given event type. If BASE is not NULL it will
  50. be set as the types base.
  51. Returns 0 if initialization was successful -1 otherwise. */
  52. int
  53. gdbpy_initialize_event_generic (PyTypeObject *type,
  54. const char *name)
  55. {
  56. if (PyType_Ready (type) < 0)
  57. return -1;
  58. return gdb_pymodule_addobject (gdb_module, name, (PyObject *) type);
  59. }
  60. /* Notify the list of listens that the given EVENT has occurred.
  61. returns 0 if emit is successful -1 otherwise. */
  62. int
  63. evpy_emit_event (PyObject *event,
  64. eventregistry_object *registry)
  65. {
  66. Py_ssize_t i;
  67. /* Create a copy of call back list and use that for
  68. notifying listeners to avoid skipping callbacks
  69. in the case of a callback being disconnected during
  70. a notification. */
  71. gdbpy_ref<> callback_list_copy (PySequence_List (registry->callbacks));
  72. if (callback_list_copy == NULL)
  73. return -1;
  74. for (i = 0; i < PyList_Size (callback_list_copy.get ()); i++)
  75. {
  76. PyObject *func = PyList_GetItem (callback_list_copy.get (), i);
  77. if (func == NULL)
  78. return -1;
  79. gdbpy_ref<> func_result (PyObject_CallFunctionObjArgs (func, event,
  80. NULL));
  81. if (func_result == NULL)
  82. {
  83. /* Print the trace here, but keep going -- we want to try to
  84. call all of the callbacks even if one is broken. */
  85. gdbpy_print_stack ();
  86. }
  87. }
  88. return 0;
  89. }
  90. static gdb_PyGetSetDef event_object_getset[] =
  91. {
  92. { "__dict__", gdb_py_generic_dict, NULL,
  93. "The __dict__ for this event.", &event_object_type },
  94. { NULL }
  95. };
  96. PyTypeObject event_object_type =
  97. {
  98. PyVarObject_HEAD_INIT (NULL, 0)
  99. "gdb.Event", /* tp_name */
  100. sizeof (event_object), /* tp_basicsize */
  101. 0, /* tp_itemsize */
  102. evpy_dealloc, /* tp_dealloc */
  103. 0, /* tp_print */
  104. 0, /* tp_getattr */
  105. 0, /* tp_setattr */
  106. 0, /* tp_compare */
  107. 0, /* tp_repr */
  108. 0, /* tp_as_number */
  109. 0, /* tp_as_sequence */
  110. 0, /* tp_as_mapping */
  111. 0, /* tp_hash */
  112. 0, /* tp_call */
  113. 0, /* tp_str */
  114. 0, /* tp_getattro */
  115. 0, /* tp_setattro */
  116. 0, /* tp_as_buffer */
  117. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  118. "GDB event object", /* tp_doc */
  119. 0, /* tp_traverse */
  120. 0, /* tp_clear */
  121. 0, /* tp_richcompare */
  122. 0, /* tp_weaklistoffset */
  123. 0, /* tp_iter */
  124. 0, /* tp_iternext */
  125. 0, /* tp_methods */
  126. 0, /* tp_members */
  127. event_object_getset, /* tp_getset */
  128. 0, /* tp_base */
  129. 0, /* tp_dict */
  130. 0, /* tp_descr_get */
  131. 0, /* tp_descr_set */
  132. offsetof (event_object, dict), /* tp_dictoffset */
  133. 0, /* tp_init */
  134. 0 /* tp_alloc */
  135. };