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