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