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