1 /* Python interface to inferior events.
2 
3    Copyright (C) 2009-2024 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 "py-event.h"
21 
22 void
evpy_dealloc(PyObject * self)23 evpy_dealloc (PyObject *self)
24 {
25   Py_XDECREF (((event_object *) self)->dict);
26   Py_TYPE (self)->tp_free (self);
27 }
28 
29 gdbpy_ref<>
create_event_object(PyTypeObject * py_type)30 create_event_object (PyTypeObject *py_type)
31 {
32   gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
33   if (event_obj == NULL)
34     return NULL;
35 
36   event_obj->dict = PyDict_New ();
37   if (!event_obj->dict)
38     return NULL;
39 
40   return gdbpy_ref<> ((PyObject *) event_obj.release ());
41 }
42 
43 /* Add the attribute ATTR to the event object EVENT.  In
44    python this attribute will be accessible by the name NAME.
45    returns 0 if the operation succeeds and -1 otherwise.  This
46    function acquires a new reference to ATTR.  */
47 
48 int
evpy_add_attribute(PyObject * event,const char * name,PyObject * attr)49 evpy_add_attribute (PyObject *event, const char *name, PyObject *attr)
50 {
51   return PyObject_SetAttrString (event, name, attr);
52 }
53 
54 /* Initialize the Python event code.  */
55 
56 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
gdbpy_initialize_event(void)57 gdbpy_initialize_event (void)
58 {
59   return gdbpy_initialize_event_generic (&event_object_type,
60                                                    "Event");
61 }
62 
63 /* Initialize the given event type.  If BASE is not NULL it will
64   be set as the types base.
65   Returns 0 if initialization was successful -1 otherwise.  */
66 
67 int
gdbpy_initialize_event_generic(PyTypeObject * type,const char * name)68 gdbpy_initialize_event_generic (PyTypeObject *type,
69                                         const char *name)
70 {
71   if (PyType_Ready (type) < 0)
72     return -1;
73 
74   return gdb_pymodule_addobject (gdb_module, name, (PyObject *) type);
75 }
76 
77 
78 /* Notify the list of listens that the given EVENT has occurred.
79    returns 0 if emit is successful -1 otherwise.  */
80 
81 int
evpy_emit_event(PyObject * event,eventregistry_object * registry)82 evpy_emit_event (PyObject *event,
83                      eventregistry_object *registry)
84 {
85   Py_ssize_t i;
86 
87   /* Create a copy of call back list and use that for
88      notifying listeners to avoid skipping callbacks
89      in the case of a callback being disconnected during
90      a notification.  */
91   gdbpy_ref<> callback_list_copy (PySequence_List (registry->callbacks));
92   if (callback_list_copy == NULL)
93     return -1;
94 
95   for (i = 0; i < PyList_Size (callback_list_copy.get ()); i++)
96     {
97       PyObject *func = PyList_GetItem (callback_list_copy.get (), i);
98 
99       if (func == NULL)
100           return -1;
101 
102       gdbpy_ref<> func_result (PyObject_CallFunctionObjArgs (func, event,
103                                                                            NULL));
104 
105       if (func_result == NULL)
106           {
107             /* Print the trace here, but keep going -- we want to try to
108                call all of the callbacks even if one is broken.  */
109             gdbpy_print_stack ();
110           }
111     }
112 
113   return 0;
114 }
115 
116 GDBPY_INITIALIZE_FILE (gdbpy_initialize_event);
117 
118 static gdb_PyGetSetDef event_object_getset[] =
119 {
120   { "__dict__", gdb_py_generic_dict, NULL,
121     "The __dict__ for this event.", &event_object_type },
122   { NULL }
123 };
124 
125 PyTypeObject event_object_type =
126 {
127   PyVarObject_HEAD_INIT (NULL, 0)
128   "gdb.Event",                                /* tp_name */
129   sizeof (event_object),                      /* tp_basicsize */
130   0,                                          /* tp_itemsize */
131   evpy_dealloc,                               /* tp_dealloc */
132   0,                                          /* tp_print */
133   0,                                          /* tp_getattr */
134   0,                                          /* tp_setattr */
135   0,                                          /* tp_compare */
136   0,                                          /* tp_repr */
137   0,                                          /* tp_as_number */
138   0,                                          /* tp_as_sequence */
139   0,                                          /* tp_as_mapping */
140   0,                                          /* tp_hash  */
141   0,                                          /* tp_call */
142   0,                                          /* tp_str */
143   0,                                          /* tp_getattro */
144   0,                                          /* tp_setattro */
145   0,                                          /* tp_as_buffer */
146   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
147   "GDB event object",                         /* tp_doc */
148   0,                                          /* tp_traverse */
149   0,                                          /* tp_clear */
150   0,                                          /* tp_richcompare */
151   0,                                          /* tp_weaklistoffset */
152   0,                                          /* tp_iter */
153   0,                                          /* tp_iternext */
154   0,                                          /* tp_methods */
155   0,                                          /* tp_members */
156   event_object_getset,                                  /* tp_getset */
157   0,                                          /* tp_base */
158   0,                                          /* tp_dict */
159   0,                                          /* tp_descr_get */
160   0,                                          /* tp_descr_set */
161   offsetof (event_object, dict),              /* tp_dictoffset */
162   0,                                          /* tp_init */
163   0                                           /* tp_alloc */
164 };
165