Commit | Line | Data |
---|---|---|
162078c8 NB |
1 | /* Python interface to inferior function events. |
2 | ||
61baf725 | 3 | Copyright (C) 2013-2017 Free Software Foundation, Inc. |
162078c8 NB |
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 "defs.h" | |
21 | #include "py-event.h" | |
abf5651e | 22 | #include "py-ref.h" |
162078c8 | 23 | |
162078c8 NB |
24 | /* Construct either a gdb.InferiorCallPreEvent or a |
25 | gdb.InferiorCallPostEvent. */ | |
26 | ||
27 | static PyObject * | |
28 | create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid, | |
29 | CORE_ADDR addr) | |
30 | { | |
7780f186 | 31 | gdbpy_ref<> event; |
162078c8 | 32 | int failed; |
162078c8 NB |
33 | |
34 | switch (flag) | |
35 | { | |
36 | case INFERIOR_CALL_PRE: | |
35c61a1d | 37 | event = create_event_object (&inferior_call_pre_event_object_type); |
162078c8 NB |
38 | break; |
39 | case INFERIOR_CALL_POST: | |
35c61a1d | 40 | event = create_event_object (&inferior_call_post_event_object_type); |
162078c8 NB |
41 | break; |
42 | default: | |
abf5651e | 43 | gdb_assert_not_reached ("invalid inferior_call_kind"); |
162078c8 NB |
44 | } |
45 | ||
7780f186 | 46 | gdbpy_ref<> ptid_obj (gdbpy_create_ptid_object (ptid)); |
162078c8 | 47 | if (ptid_obj == NULL) |
abf5651e | 48 | return NULL; |
162078c8 | 49 | |
abf5651e TT |
50 | if (evpy_add_attribute (event.get (), "ptid", ptid_obj.get ()) < 0) |
51 | return NULL; | |
162078c8 | 52 | |
7780f186 | 53 | gdbpy_ref<> addr_obj (PyLong_FromLongLong (addr)); |
162078c8 | 54 | if (addr_obj == NULL) |
abf5651e | 55 | return NULL; |
162078c8 | 56 | |
abf5651e TT |
57 | if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0) |
58 | return NULL; | |
162078c8 | 59 | |
abf5651e | 60 | return event.release (); |
162078c8 NB |
61 | } |
62 | ||
63 | /* Construct a gdb.RegisterChangedEvent containing the affected | |
64 | register number. */ | |
65 | ||
66 | static PyObject * | |
67 | create_register_changed_event_object (struct frame_info *frame, | |
68 | int regnum) | |
69 | { | |
7780f186 | 70 | gdbpy_ref<> event (create_event_object (®ister_changed_event_object_type)); |
162078c8 NB |
71 | if (event == NULL) |
72 | return NULL; | |
73 | ||
7780f186 | 74 | gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame)); |
162078c8 | 75 | if (frame_obj == NULL) |
abf5651e | 76 | return NULL; |
162078c8 | 77 | |
abf5651e TT |
78 | if (evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0) |
79 | return NULL; | |
162078c8 | 80 | |
7780f186 | 81 | gdbpy_ref<> regnum_obj (PyLong_FromLongLong (regnum)); |
162078c8 | 82 | if (regnum_obj == NULL) |
abf5651e | 83 | return NULL; |
162078c8 | 84 | |
abf5651e TT |
85 | if (evpy_add_attribute (event.get (), "regnum", regnum_obj.get ()) < 0) |
86 | return NULL; | |
162078c8 | 87 | |
abf5651e | 88 | return event.release (); |
162078c8 NB |
89 | } |
90 | ||
91 | /* Construct a gdb.MemoryChangedEvent describing the extent of the | |
92 | affected memory. */ | |
93 | ||
94 | static PyObject * | |
95 | create_memory_changed_event_object (CORE_ADDR addr, ssize_t len) | |
96 | { | |
7780f186 | 97 | gdbpy_ref<> event (create_event_object (&memory_changed_event_object_type)); |
162078c8 NB |
98 | |
99 | if (event == NULL) | |
100 | return NULL; | |
162078c8 | 101 | |
7780f186 | 102 | gdbpy_ref<> addr_obj (PyLong_FromLongLong (addr)); |
162078c8 | 103 | if (addr_obj == NULL) |
abf5651e | 104 | return NULL; |
162078c8 | 105 | |
abf5651e TT |
106 | if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0) |
107 | return NULL; | |
162078c8 | 108 | |
7780f186 | 109 | gdbpy_ref<> len_obj (PyLong_FromLong (len)); |
162078c8 | 110 | if (len_obj == NULL) |
abf5651e | 111 | return NULL; |
162078c8 | 112 | |
abf5651e TT |
113 | if (evpy_add_attribute (event.get (), "length", len_obj.get ()) < 0) |
114 | return NULL; | |
162078c8 | 115 | |
abf5651e | 116 | return event.release (); |
162078c8 NB |
117 | } |
118 | ||
119 | /* Callback function which notifies observers when an event occurs which | |
120 | calls a function in the inferior. | |
121 | This function will create a new Python inferior-call event object. | |
122 | Return -1 if emit fails. */ | |
123 | ||
124 | int | |
125 | emit_inferior_call_event (inferior_call_kind flag, ptid_t thread, | |
126 | CORE_ADDR addr) | |
127 | { | |
162078c8 NB |
128 | if (evregpy_no_listeners_p (gdb_py_events.inferior_call)) |
129 | return 0; | |
130 | ||
7780f186 | 131 | gdbpy_ref<> event (create_inferior_call_event_object (flag, thread, addr)); |
162078c8 | 132 | if (event != NULL) |
abf5651e | 133 | return evpy_emit_event (event.get (), gdb_py_events.inferior_call); |
162078c8 NB |
134 | return -1; |
135 | } | |
136 | ||
137 | /* Callback when memory is modified by the user. This function will | |
138 | create a new Python memory changed event object. */ | |
139 | ||
140 | int | |
141 | emit_memory_changed_event (CORE_ADDR addr, ssize_t len) | |
142 | { | |
162078c8 NB |
143 | if (evregpy_no_listeners_p (gdb_py_events.memory_changed)) |
144 | return 0; | |
145 | ||
7780f186 | 146 | gdbpy_ref<> event (create_memory_changed_event_object (addr, len)); |
162078c8 | 147 | if (event != NULL) |
abf5651e | 148 | return evpy_emit_event (event.get (), gdb_py_events.memory_changed); |
162078c8 NB |
149 | return -1; |
150 | } | |
151 | ||
152 | /* Callback when a register is modified by the user. This function | |
153 | will create a new Python register changed event object. */ | |
154 | ||
155 | int | |
156 | emit_register_changed_event (struct frame_info* frame, int regnum) | |
157 | { | |
162078c8 NB |
158 | if (evregpy_no_listeners_p (gdb_py_events.register_changed)) |
159 | return 0; | |
160 | ||
7780f186 | 161 | gdbpy_ref<> event (create_register_changed_event_object (frame, regnum)); |
162078c8 | 162 | if (event != NULL) |
abf5651e | 163 | return evpy_emit_event (event.get (), gdb_py_events.register_changed); |
162078c8 NB |
164 | return -1; |
165 | } |